Access Millions of academic & study documents

8 16 (Searching for Substrings) Write a program that inputs a line o

Content type
User Generated
Showing Page:
1/3
8.16 (Searching for Substrings) Write a 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. [Hint: The
second call to strstr should contain searchPtr + 1 as its
first argument . Also, remember to include the C string
header to use the strstr function.]
help in c please 1 #include 2 #include 4 6 int main (void)
7 8 char Text [80]; char Search [20]; char* searchPtr; printf
(\"Enter a line of text:\"); 10 12 13 14 15 gets (Text);
printf(\"Enter your search string:\"); 17 18 19 20 21 scanf (
\"%15\", Search); strstr(Text, Search); = searchPtr if
(searchPtr) 23 24 25 26 27 28 29 ( \"\ %s\ %s \" , Search,
searchPtr); printf searchPtr- strstr searchPtr 1, Search); if
(searchPtr) 30 31 printf (\"\ %s\ %s \" , Search, searchPtr);
32 return ; 34 35
Solution
#include<stdio.h>
#include<string.h>
int main()

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/3
{
char text[200];
char search[50];
char* searchPtr = NULL;
int i=0;
printf(\"Enter a line of text : \");
gets(text);
printf(\"Enter search string : \");
gets(search);
searchPtr = strstr(text, search);
while(searchPtr!=NULL)
{
i = i+1;
printf(\"At %d occurence remaining string is %s \
\",i,searchPtr);
searchPtr = strstr(searchPtr+1, search);
}
return 0;

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/3

Sign up to view the full document!

lock_open Sign Up
Unformatted Attachment Preview
8.16 (Searching for Substrings) Write a 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. [Hint: The second call to strstr should contain searchPtr + 1 as its first argument . Also, remember to include the C string header to use the strstr function.] help in c please 1 #include 2 #include 4 6 int main (void) 7 8 char Text [80]; char Search [20]; char* searchPtr; printf (\"Enter a line of text:\"); 10 12 13 ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Indeed
4.5
Sitejabber
4.4

Similar Documents