(Searching for Characters) Write a program that inputs several lines of text and a search character and uses function strchr to determine the total number of occurrences of the character in the lines of text.

What will be an ideal response?

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

const int SIZE = 80;

int main()
{
char text[ 3 ][ SIZE ];
char search;
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 ], SIZE );

// 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 character: ";
cin >> search;

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

while ( searchPtr = strchr( 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 character: e
The total number of occurrences of 'e' in the text is: 10

Computer Science & Information Technology

You might also like to view...

A nested IF requires one or more IF functions joined by an AND function

Indicate whether the statement is true or false.

Computer Science & Information Technology

A hard reset

A) Cannot be performed by a user B) Is done by unplugging the mobile device C) Is easily done with a reboot of the system D) Is also called a factory reset

Computer Science & Information Technology