(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.
What will be an ideal response?
```
#include
#include
using namespace std;
const int SIZE1 = 80;
const int SIZE2 = 15;
int main()
{
char text[ SIZE1 ]; // char array for text input
char search[ SIZE2 ]; // char array for searching
char *searchPtr;
// ask user for text input
cout << "Enter a line of text:\n";
cin.get( text, SIZE1 );
// ask for a search string
cout << "\nEnter a search string: ";
cin >> search;
// demonstrates usage of function strstr to locate occurrences
// of search in text
searchPtr = strstr( text, search );
if ( searchPtr )
{
cout << "\nThe remainder of the line beginning with\n"
<< "the first occurrence of \"" << search << "\":\n"
<< searchPtr << '\n';
searchPtr = strstr( searchPtr + 1, search );
if ( searchPtr ) // second occurrence of search string
cout << "\nThe remainder of the line beginning with"
<< "\nthe second occurrence of \"" << search << "\":\n"
<< searchPtr << '\n';
else
cout << "\nThe search string appeared only once.\n";
} // end if
else
cout << "\"" << search << "\" not found.\n";
return 0;
} // end main
```
Enter a line of text:
I like to eat apples and bananas
Enter a search string: an
The remainder of the line beginning with
the first occurrence of "an":
and bananas
The remainder of the line beginning with
the second occurrence of "an":
ananas
You might also like to view...
Which of the following choices completes the sentence correctly? A(n)______ is a(n) _______.
a. JToggleButton, JCheckBox b. JToggleButton, JRadioButton c. JButton, JToggleButton d. JButton, AbstractButton
Old computers, cell phones, TVs, and e-readers that are disposed of are referred to as ________
Fill in the blank(s) with correct word
What would the browser display if the following code is executed in a script?
``` var x = 11, y = 14; if ( x > 13 ) if ( y > 13 ) document.writeln( "x and y are > 13" ); else document.writeln( "x is <= 13" ); ``` a) nothing, the code will generate an error b) 11 c) x and y are > 13 d) x is <= 13
A(n) ____ statement stores the value of the right-hand side of the expression in the memory location of the left-hand side.
A. construct B. arithmetic C. equals D. assignment