fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define SIZE 300
  5.  
  6. int main(void)
  7. {
  8. char sentence[SIZE];
  9. char searchString[SIZE];
  10.  
  11. printf("Enter a line of text:\n");
  12. fgets(sentence, SIZE, stdin);
  13.  
  14. printf("Enter the search string:\n");
  15. fgets(searchString, SIZE, stdin);
  16.  
  17. sentence[strcspn(sentence, "\n")] = '\0';
  18. searchString[strcspn(searchString, "\n")] = '\0';
  19.  
  20. char *searchPtr =
  21. strstr(sentence, searchString);
  22.  
  23. if (searchPtr == NULL)
  24. {
  25. printf("The search string was not found.\n");
  26. }
  27. else
  28. {
  29. printf("\nThe first occurrence is:\n");
  30. printf("%s\n", searchPtr);
  31.  
  32. searchPtr =
  33. strstr(searchPtr + 1, searchString);
  34.  
  35. if (searchPtr != NULL)
  36. {
  37. printf("\nThe second occurrence is:\n");
  38. printf("%s\n", searchPtr);
  39. }
  40. else
  41. {
  42. printf("\nThere is no second occurrence.\n");
  43. }
  44. }
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 5348KB
stdin
Standard input is empty
stdout
Enter a line of text:
Enter the search string:
The search string was not found.