I'm creating 2 strings. The first is a sentence, the second is a word. If the sentence contains the word, we erase it from the sentence.
I've tried it in several ways but it always gives the correct answer if the word is at the end of the sentence.
char sntc[150];
char word[30];
gets(sntc);
gets(word);
char temp[50];
int i=0;
int index=0;
while (i<strlen(sntc);) {
for(; sntc[i] != '\0'; i++) {
if(sntc[i] == ' ' || sntc[i] == '\0') {
break;
}
temp[index++]=sntc[i];
}
temp[index]='\0';
if (strcmp(temp, word) == 0) {
i++;
index=0;
continue;
} else {
printf("%s ", temp);
i++;
index=0;
}
}
For this input:
merhaba dunyali nasilsin
dunyali
the expected output is:
merhaba nasilsin