Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in C++ that will read in a line of text and output (1) the numbe

ID: 3546122 • Letter: W

Question

Write a program in C++ that will read in a line of text and output (1) the number of
words in the line; and (2) the number of occurrences of each letter in alphabetical
order using a string. You can assume that the input consists entirely of letters, whitespace, commas, and periods. When outputting the number of letters that occur in a line, be sure to count uppercase and lowercase versions of a letter as the same letter. Output the letters in alphabetical order and list only those letters that occur in the input line.

For example, if your input line is
I say Hi.

the output of your program should be similar to the following:
3 words, 1 a, 1 h, 2 i, 1 s, 1 y.

Hints for question 1:
To count and store the occurrence of each individual letter, create an array of 26
integers, where the ?rst element will store the number of occurrences of the letter a or A, the second for b or B, and so on. Then utilize the fact that a char such as a can be treated as an integer. For instance, a-a is equal to 0. This latter fact will allow you to quickly identify the element in the array created above for a given letter.

Explanation / Answer

Here are two functions that will get you started. One parses the buffer passed in filling in the count array withcharacter counts. It returns the number of words. Any punctuation, not just commas and periods, delimit words, but since the assignment guarenteed that the input would have no other punctuation other than commas and periods this is safe.

The print function runs the count array and prints only non-zero counts with the corresponding printable character. All alph characters are translated to uppercase so aAa is counted as three As.

#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "ctype.h"

/* counts assumed to be an array of 255 integers
returns the number of words and the count array filled in
*/
int parse( char *buf, int *counts )
{
char *cp; /* pointer to walk the buffer */
int wc = 0; /* word count */
int between_words = 0; /* between wods? */

memset( counts, 0, sizeof( int ) * 255 ); /* set all counts to zero */

for( cp = buf; *cp; cp++ ) /* for each character */
{
if( ispunct(*cp) || isspace(*cp) )
{
if( !between_words )
wc++; /* up word count on first non-word character */
between_words = 1;
}
else
{
between_words = 0;
if( isalpha(*cp) ) /* allow numbers to make up words, but not be counted */
counts[toupper(*cp)]++; /* incr the count for this letter */
}
}


return wc;
}

/* this function will print the counts for any 'printable' character if the
count is not zero. Printable characters are considered to be greater
or equal to a blank, and less or equal to 0x7f (del)
*/
int print_count( int *counts )
{
int i;

for( i = 0x20; i < 0x80; i++ )
if( counts[i] )
printf( "%c = %d ", (unsigned char) i, counts[i] );
}

int main( int argc, char **argv )
{

int wc;
int counts[255];

wc = parse( argv[1], counts );
printf( "the line contains %d words ", wc );
print_count( counts );
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote