Write a C program to remove all occurrence of the word from entered string.
#include <stdio.h>
#include <string.h>
int main() {
char string[256], text[256], words[100][256];
int i, j, k, n, flag;
i = j = k = 0;;
/* get the input string from the user */
printf("Enter your input string:");
fgets(string, 256, stdin);
string[strlen(string) - 1] = '\0';
/* get the word that needs to be removed from i/p string */
printf("Enter the word your want to remove:");
fgets(text, 256, stdin);
text[strlen(text) - 1] = '\0';
/* copying each and every word from the string */
while (string[i] != '\0') {
if (string[i] == ' ') {
words[j][k] = '\0';
k = 0;
j++;
} else {
words[j][k++] = string[i];
}
i++;
}
words[j][k] = '\0';
#include <string.h>
int main() {
char string[256], text[256], words[100][256];
int i, j, k, n, flag;
i = j = k = 0;;
/* get the input string from the user */
printf("Enter your input string:");
fgets(string, 256, stdin);
string[strlen(string) - 1] = '\0';
/* get the word that needs to be removed from i/p string */
printf("Enter the word your want to remove:");
fgets(text, 256, stdin);
text[strlen(text) - 1] = '\0';
/* copying each and every word from the string */
while (string[i] != '\0') {
if (string[i] == ' ') {
words[j][k] = '\0';
k = 0;
j++;
} else {
words[j][k++] = string[i];
}
i++;
}
words[j][k] = '\0';
/* print all words except the word that needs to be removed */
for (i = 0; i <= j; i++) {
if (strcmp(words[i], text) != 0) {
printf("%s ", words[i]);
}
}
printf("\n");
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input string:Hai Hello.. How are you?
Enter the word your want to remove:Hai
Hello.. How are you?
Enter your input string:Hai Hello.. How are you?
Enter the word your want to remove:Hai
Hello.. How are you?
Not working
ReplyDelete