164k views
1 vote
Write a complete C program that inputs a line of text and a search string from the keyboard. Using function strstr, locate the first occurrence of the search string in the line of text, and assign the location to variable searchPtr of type char *. If the search string is found, print the remainder of the line of text beginning with the search string. Then, use strstr again to locate the next occurrence of the search string in the line of text. If a second occurrence is found, print the remainder of the line of text beginning with the second occurrence. Also, determine the total occurrences of the string in the lines of text. Print the results.

1 Answer

5 votes

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.

Write a complete C program that inputs a line of text and a search string from the-example-1
Write a complete C program that inputs a line of text and a search string from the-example-2
User Jason Orendorff
by
4.6k points