title: Function to delete all occurrences of a word in a sentence in C
body: <p>I have this code which will remove the first occurrence of the word from the sentence:</p>
  
  <pre><code>#include "stdio.h"
  #include "string.h"
  
  int delete(char *source, char *word);
  
  void main(void) {
  
      char sentence[500];
      char word[30];
  
  
  
      printf("Please enter a sentence. Max 499 chars. \n");
      fgets(sentence, 500, stdin);
  
      printf("Please enter a word to be deleted from sentence. Max 29 chars. \n");
      scanf("%s", word);
  
      delete(sentence, word);
  
      printf("%s", sentence);
  }
  
  
  int delete(char *source, char *word) {
  
      char *p;
      char temp[500], temp2[500];
  
      if(!(p = strstr(source, word))) {
          printf("Word was not found in the sentence.\n");
          return 0;
      }
  
      strcpy(temp, source);
      temp[p - source] = '\0';
      strcpy(temp2, p + strlen(word));
      strcat(temp, temp2);    
      strcpy(source, temp);
      return 1;
  }
  </code></pre>
  
  <p>How would I modify it to delete all occurrences of the word in the given sentence? Can i still use the strstr function in this case?</p>
  
  <p>Thanks for the help!</p>
  
  <p>Open to completely different ways of doing this too.</p>
  
  <p>P.S. This might sound like a homework question, but it's actually a past midterm question which I'd like to resolve to prepare for my midterm!</p>
  
  <p>As a side question, if I use <code>fgets(word, 30, stdin)</code> instead of <code>scanf("%s", word)</code>, it no longer works and tells me that the word was not found in the sentence. Why?</p>