(Searching for Characters) Write a program based on the program of Exercise 22.25 that inputs several lines of text and uses function strchr to determine the total number of occurrences of each letter of the alphabet in the text. Uppercase and lowercase letters should be counted together. Store the totals for each letter in an array, and print the values in tabular format after the totals have
been determined.
What will be an ideal response?
```
#include
#include
#include
#include
using namespace std;
const int SIZE1 = 80;
const int SIZE2 = 26;
int main()
{
char text[ 3 ][ SIZE1 ];
char *searchPtr;
int characters[ SIZE2 ] = { 0 };
cout << "Enter three lines of text:\n";
for ( int i = 0; i <= 2; i++ )
cin.getline( &text[ i ][ 0 ], SIZE1 );
// convert letters to lowercase
for ( int k = 0; k <= 2; k++ )
for ( int j = 0; text[ k ][ j ] != '\0'; j++ )
{
char c = static_cast< char >( tolower( text[ k ][ j ] ) );
text[ k ][ j ] = c;
} // end for
// run through the alphabet
for ( int q = 0; q < SIZE2; q++ )
{
for ( int j = 0; j <= 2; j++ )
{
searchPtr = &text[ j ][ 0 ];
while ( searchPtr = strchr( searchPtr, 'a' + q ) )
{
characters[ q ]++; // increment each time the character occurs
searchPtr++;
} // end while
} // end inner for
} // end outer for
cout << "\nThe total number of occurrences of each character:\n";
for ( int w = 0; w < SIZE2; w++ )
cout << setw( 3 ) << static_cast< char >( 'a' + w ) << ':'
<< setw( 3 ) << characters[ w ] << '\n';
return 0;
} // end main
```
Enter three lines of text:
The first line of text
The second line of text
The third line of text
The total number of occurrences of each character:
a: 0
b: 0
You might also like to view...
How many of the current suppliers do not provide food items to the resort and spa? How could the chef use this information?
What will be an ideal response?
Which of the following is false for pointer-based strings?
a. A string may include letters, digits and various special characters (i.e., +, -, *). b. A string in C++ is an array of characters ending in the null character ('\0'). c. String literals are written inside of single quotes. d. A string may be assigned in a declaration to either a character array or a variable of type char *.