Next are description about this exercise: (Searching for Substrings) Write a pro
ID: 3625904 • Letter: N
Question
Next are description about this exercise:
(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 occurence is found, print the remainder of the line of text beginning with second occurence.
(The second call to strstr should contain the expression searchPtr + 1 as its first argument.)
Explanation / Answer
please rate - thanks
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
char sent[81];
char find[81];
char *ptr;
int i;
int count=0;
cout<<"Enter line of text "<<": ";
cin.getline(sent,81 );
for(i=0;sent[i]!='';i++)
sent[i]=tolower(sent[i]);
cout<<"Enter a string to look for: ";
cin>>find;
ptr=strstr(sent,find);
if(ptr)
{ count++;
cout<<"after 1st occurance remaining text= "<<ptr<<endl;
ptr=strstr(ptr+1,find);
if(ptr!=NULL)
{cout<<"after second occurance remaining text= "<<ptr<<endl;
count++;
}
}
else
cout<<"no occurances of "<<find<<" in "<<sent<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.