(Searching for Substrings) Write a program based on the program of Exercise 22.23 that in- puts several lines of text and a search string, then uses function strstr to determine the total num- ber of occurrences of the string in the lines of text. Print the result.

What will be an ideal response?


```
#include
#include
#include
using namespace std;

const int SIZE1 = 80;
const int SIZE2 = 20;

int main()
{
char text[ 3 ][ SIZE1 ];
char search[ SIZE2 ];
char *searchPtr;
int count = 0;
int i;

cout << "Enter three lines of text:\n";

for ( i = 0; i <= 2; i++ )
cin.getline( &text[ i ][ 0 ], SIZE1 );

// make all characters lowercase
for ( i = 0; i <= 2; i++ )

for ( int j = 0; text[ i ][ j ] != '\0'; j++ )
{
char c = static_cast< char >( tolower( text[ i ][ j ] ) );
text[ i ][ j ] = c;
} // end for

cout << "\nEnter a search string: ";
cin >> search;

for ( i = 0; i <= 2; i++ )
{
searchPtr = &text[ i ][ 0 ];

while ( searchPtr = strstr( searchPtr, search ) )
{
++count;
++searchPtr;
} // end while
} // end for

cout << "\nThe total number of occurrences of \"" << search
<< "\" in the text is: " << count << endl;

return 0;
} // end main
```
Enter three lines of text:
The first line of text
The second line of text
The third line of text
Enter a search string: line
The total number of occurrences of "line" in the text is: 3

Computer Science & Information Technology

You might also like to view...

A JTextArea ________.

a. provides a bounding box used for the layout of JTextFields b. wraps lines by default c. provides an area for manipulating multiple lines of text d. provides scrollbars for JTextFields

Computer Science & Information Technology

Analyze the following code.

``` // Program 1: public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new A(); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } // Program 2: public class Test { public static void main(String[] args) { A a1 = new A(); A a2 = new A(); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } ``` a. Program 1 displays true and Program 2 displays true b. Program 1 displays false and Program 2 displays true c. Program 1 displays true and Program 2 displays false d. Program 1 displays false and Program 2 displays false

Computer Science & Information Technology

Which keyboard shortcut displays the Save As dialog box?

A) Shift + S B) F2 C) F12 D) Ctrl + S

Computer Science & Information Technology

Match each of the following Windows 10 apps/tools to their meanings:

I. Photos II. PC Reset III. Groove Music IV. News V. Movies & TV A. displays photo albums from the Pictures folder, from a OneDrive, and from other connected devices B. can play the music that exists on a PC C. used to buy or rent movies and episodes of TV shows D. can provide customized world and national news E. brings a computer back to its original factory settings

Computer Science & Information Technology