{}
run-icon
main.cpp
#include <stdio.h> #include <string.h> int main() { char str[100], word[20]; int i, j, lw, temp, chk = 0; printf("Enter the main string: "); fgets(str, sizeof(str), stdin); printf("Enter the word to remove: "); fgets(word, sizeof(word), stdin); str[strcspn(str, "\n")] = 0; word[strcspn(word, "\n")] = 0; int ls = strlen(str); lw = strlen(word); for (i = 0; i <= ls - lw; i++) { temp = i; for (j = 0; j < lw; j++) { if (str[i + j] != word[j]) { break; } } if (j == lw) { chk = 1; break; } } if (chk == 1) { for (j = temp; j < ls - lw; j++) { str[j] = str[j + lw]; } str[ls - lw] = '\0'; } printf("New String = %s\n", str); return 0; }
Output