(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

Computer Science & Information Technology

You might also like to view...

What's the purpose of adding editable metadata?

What will be an ideal response?

Computer Science & Information Technology

The _________tab of the Internet Properties dialog box enables you to configure cookie handling, location services, the pop-up blocker, and InPrivate browser settings

Fill in the blank(s) with correct word

Computer Science & Information Technology