Output of the letter count is a loop running from 0-25 with


Count words and letters

Write a program that reads in a line of text, counts and outputs the number of words in the line and the number of occurrences of each letter.

Define a word to be a string of letters delimited by white space (blank, newline, tab), a comma, or a period. Assume that the input consists only of characters and these delimiters. For purposes of counting letters, case is immaterial.

Output letters in alphabetic order and only output those letters that occur.

Some Help with your Algorithm development:

The word count is carried out with a state machine. We enter with our state variable, inWord set to false, and our word count set to0.

while(input characters is successful)

if we have encountered a blank, newline or a tab,

we set state to false

else if inWord is false,

set state to true

increment word count

lowCase = tolower(inChar);

charCount[int(lowCase) - int('a')]++;

cout << wordCount << " " words" << endl;

for(i = 0; i < 25; i++)

if(charCount[i] != 0)

cout << charCount[i] << " " << char(i + 'a')<< endl;

Comments on the letter count code:

We run tolower() on all characters entered. The data structure for the letter count is a 26-letter int array with indices in the range 0-25, calculated by

index = int(character) - int('a')

as each letter is read in, increment the appropriate array element.

Output of the letter count is a loop running from 0-25, with an if statement that allows output if the array entry isn't zero.

 

Request for Solution File

Ask an Expert for Answer!!
Computer Engineering: Output of the letter count is a loop running from 0-25 with
Reference No:- TGS0662368

Expected delivery within 24 Hours