Question
Write a program that counts how often the 26 letters of the (Latin) alphabet occur in an input text. For this, your program reads from
Write a program that counts how often the 26 letters of the (Latin) alphabet occur in an input text. For this, your program reads from an input stream until its end is reached. The letters 'a' to 'z' and 'A' to 'Z' are counted. All other characters are simply ignored. Upper case and lower case are to be treated together; e.g., the string "aA" should result in the letter 'a' being counted twice.
The letters 'a', 'e', 'i', 'o', 'u' (and their upper-case counterparts) are referred to as vowels. All other letters as consonants. After counting, your program should print (in lower case) the most frequent vowel, the most frequent consonant, and the most frequent letter overall. Whenever multiple letters are equally the most frequently used ones, print the letter that comes first, alphabetically, and ignore the other ones. Correct executions of your program look like this:
C++ rocks, or C++ sucks: you decide!
Most frequent vowel: o (3 times)
Most frequent consonant: c (5 times)
Most frequent letter, overall: c (5 times)
First
sEcond
thIrd
LINE
Most frequent vowel: i (3 times)
Most frequent consonant: d (2 times)
Most frequent letter, overall: i (3 times)
dada
Most frequent vowel: a (2 times)
Most frequent consonant: d (2 times)
Most frequent letter, overall: a (2 times)
dodo
Most frequent vowel: o (2 times)
Most frequent consonant: d (2 times)
Most frequent letter, overall: d (2 times)
(Note the difference between the last two examples.)
So far, you have seen examples of the program reading from std::cin. Actually, this is what your program should do whenever it is called without command line parameters. When it is called with such a command line parameter, it should treat the parameter as the name of a file to be opened and read instead of std::cin. Examples:
./letterstats lorem
Most frequent vowel: e (373 times)
Most frequent consonant: t (300 times)
Most frequent letter, overall: e (373 times)
./letterstats letterstats.cpp
Most frequent vowel: e (262 times)
Most frequent consonant: t (267 times)
Most frequent letter, overall: t (267 times)
Hint: you can define a function as follows that can be called with either std::cin or a std::ifstream
void processInput(std::istream& input)
{
...
}
Your program must print error messages, in case either the file could not be opened, or there was more than one command line parameter:
./letterstats loremm
cannot open input file loremm
./letterstats lorem ipsum
cannot handle parameter list
Step by Step Solution
3.45 Rating (148 Votes )
There are 3 Steps involved in it
Step: 1
The answer provided below has been developed in a clear step b...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started