0

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
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

3

Search for the word and overwrite it with the part you want to keep:

char *found;
while((found=strstr(sntc,word))!=NULL)
  strcpy(found,found+strlen(word));

Of course you could store strlen(word) in a variable.
Test: https://ideone.com/BpLRe3

If you are worried about the end of the Universe, use memmove (and also use fgets, this one applies even if you are not worried). Both of them require a bit of work as fgets also stores the line-break, which has to be disposed (otherwise strstr would look for something like word\n, presumably in vain), while memmove moves memory, so the size has to be told explicitly, and it has to include the null terminator:

char sntc[150];
char word[30];
char *found;
size_t wordlen;
fgets(sntc,sizeof(sntc),stdin);
fgets(word,sizeof(word),stdin);
wordlen=strlen(word)-1;
word[wordlen]=0;
while((found=strstr(sntc,word))!=NULL)
  memmove(found,found+wordlen,strlen(found+wordlen)+1);
printf("%s",sntc);

The space-trick is still applied in the test: https://ideone.com/hvZAtm (I've put "_dunyali" into the second line of input, just with a space - here the formatter apparently ate that space, that is why it is marked with an underscore now), but in real-life you would have to take care about spaces (one of which can usually removed) and punctuation (which may or may not be needed afterwards) surrounding the word.

tevemadar
  • 12,389
  • 3
  • 21
  • 49
  • 1
    Beware: C11 [§7.24.2.3 The `strcpy` function](https://port70.net/~nsz/c/c11/n1570.html#7.24.2.3) says: _If copying takes place between objects that overlap, the behavior is undefined._ For the sample data, I think the objects overlap (but if the trailing word was 'akşam', it wouldn't overlap). Better to use `memmove()` which works with overlapping data. – Jonathan Leffler May 12 '19 at 23:39
  • @JonathanLeffler yeah... It is lovely when existing trivial implementations are taken into consideration by someone writing a standard, but (the same or another) someone is just too lazy to write a longer half-sentence about it. I wonder if there really is an implementation which blows up on this kind of overlap (copying from higher addresses to lower ones). – tevemadar May 13 '19 at 10:48
  • Probably not many, which is part of why it’s a comment. But I haven’t tested all platforms. Have you run a test? How do you avoid double spaces where the word was deleted? – Jonathan Leffler May 13 '19 at 14:56
  • @JonathanLeffler sorry, but no and no. I do not have a test for all possible platforms, and I do not have a simple solution for the double-space thing, instead I have a space as part of `word` in both examples and a short mention in the description. – tevemadar May 13 '19 at 15:15
  • I’ll stop lobbing defused hand grenades. What you’ve got is probably OK in most situations. Tweaks for odd (but realistic; consider removing a word which is part of a longer word) situations are probably not relevant to the classroom exercise. – Jonathan Leffler May 13 '19 at 15:21