Answer:
Here is the C program:
#include <stdio.h> // to use input output functions
#include <string.h> // to manipulate strings
int main() //start of main() function body
{ char input_text[100]; // array to store the input text line
char search_string[50]; // char type array to store string to be searched
char *searchPtr;
// char type pointer variable to search for search_string in input_text
printf("Input a text line \\"); // prompts user to enter a line
fgets(input_text,100,stdin); // to get line of input text from user
printf("Input a string to search: ");
//prompts user to enter string to be searched
scanf("%s", search_string); //reads the value of search_string
//search for the search_string in input_text
searchPtr = strstr(input_text, search_string);
if (searchPtr) //if string to be searched is found
{ printf("\\The text line beginning with the first occurrence of %s: ", search_string);
//prints the first occurrence of that search_string in input_text
printf("%s\\", searchPtr);
//search for the search_string in the rest of the input_text
searchPtr = strstr(searchPtr + 1, search_string);
if (searchPtr) // if search_string is found in input_text again {
printf("\\The text line beginning with the second occurrence of %s:\\", search_string);
//display the second occurrence of the search_string in input_text
printf("%s\\", searchPtr); }
else //if search_string only appeared once in input_text
printf("The string to be searched just appeared once.\\"); }
else //if string to be searched is not found anywhere in input_text
printf("\"%s\" cannot be found.\\", search_string); }
Step-by-step explanation:
The program first prompts the user to enter a line of text and a string to be searched in the line. When the user enters text and search string then search string is located in input text using strstr() function. If the first occurrence of search string is found then the remainder of the line of text beginning with the search string is displayed. Then strstr is used again to locate the next occurrence of the search string in the input_text.
The output of program is attached.