logo CodeStepByStep logo

vowelStats

Language/Type: C++ basics streams file input
Related Links:

Write a function named vowelStats that accepts a string parameter representing a file name, then opens/reads that file's contents and prints information to the console about the file's lines. Report the length of each line along with the number of vowels (AEIOU, case-insensitive) on each line. At the end of your output, show the number of lines in the file, the length of the longest line, and the average characters per line, in exactly the format shown below. For example, if the input file carroll.txt contains the following data:

Beware the Jabberwock, my son,
the jaws that bite, the claws that catch,

Beware the JubJub bird and shun
the frumious bandersnatch.

Then the call of vowelStats("carroll.txt"); should produce the following console output:

Line 1 has 30 chars and 8 vowels
Line 2 has 41 chars and 9 vowels
Line 3 has 0 chars and 0 vowels
Line 4 has 31 chars and 9 vowels
Line 5 has 26 chars and 8 vowels
5 lines; longest = 41, average = 25.6

You may assume that the input file exists and is readable. You may also assume that the file contains at least one line of input.

Constraints: Your solution should read the file only once, not make multiple passes over the file data.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.