fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void removeWord(char * str, char * toRemove);
  5.  
  6. int main(){
  7. char words[100];
  8. char origional[100];
  9. char removeword[100];
  10. printf("Enter your sentence: ");
  11. gets(words);
  12. printf("Enter word to delete: ");
  13. gets(removeword);
  14. strcpy(origional, words);
  15. removeWord(words, removeword);
  16. printf("\nWord before deleting: %s", origional);
  17. printf("\nWord after deleting: %s", words);
  18.  
  19. }
  20.  
  21. void removeWord(char * str, char * toRemove)
  22. {
  23. int i, j, stringLen, toRemoveLen;
  24. int found;
  25.  
  26. stringLen = strlen(str);
  27. toRemoveLen = strlen(toRemove);
  28.  
  29.  
  30. for(i=0; i <= stringLen - toRemoveLen; i++)
  31. {
  32. found = 1;
  33. for(j=0; j<toRemoveLen; j++)
  34. {
  35. if(str[i + j] != toRemove[j])
  36. {
  37. found = 0;
  38. break;
  39. }
  40. }
  41.  
  42. if(str[i + j] != ' ' && str[i + j] != '\t' && str[i + j] != '\n' && str[i + j] != '\0')
  43. {
  44. found = 0;
  45. }
  46.  
  47.  
  48. if(found == 1)
  49. {
  50. for(j=i; j<=stringLen - toRemoveLen; j++)
  51. {
  52. str[j] = str[j + toRemoveLen];
  53. }
  54.  
  55. stringLen = stringLen - toRemoveLen;
  56.  
  57. i--;
  58. }
  59. }
  60. }
Success #stdin #stdout 0s 4452KB
stdin
Hello there how are you?
how
stdout
Enter your sentence: Enter word to delete: 
Word before deleting: Hello there how are you?
Word after deleting: Hello there  are you?