fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define SIZE 300
  5.  
  6. int main(void)
  7. {
  8. char text[SIZE];
  9. char searchString[SIZE];
  10.  
  11. int totalOccurrences = 0;
  12.  
  13. printf("Enter a line of text:\n");
  14. fgets(text, SIZE, stdin);
  15.  
  16. printf("Enter the search string:\n");
  17. fgets(searchString, SIZE, stdin);
  18.  
  19. text[strcspn(text, "\n")] = '\0';
  20. searchString[strcspn(searchString, "\n")] = '\0';
  21.  
  22. char *searchPtr = text;
  23.  
  24. while ((searchPtr =
  25. strstr(searchPtr, searchString)) != NULL)
  26. {
  27. ++totalOccurrences;
  28.  
  29. searchPtr += 1;
  30. }
  31.  
  32. printf("\nThe search string occurred %d times.\n",
  33. totalOccurrences);
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 5340KB
stdin
Standard input is empty
stdout
Enter a line of text:
Enter the search string:

The search string occurred 0 times.