logo CodeStepByStep logo


Language/Type: VB file input

Write a subroutine named WordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines total number of words the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet the average number of words per line (as a real number rounded to the nearest tenth) and the average word length (also rounded to the nearest tenth). For example, suppose the file tobe.txt contains the following text:

To be  or not  TO BE,  

really   the  question.

For the purposes of this problem, we will use whitespace to separate words. That means that some words include punctuation, as in "be,". For the input above, the call of WordStatsPlus("tobe.txt") should produce exactly the following output. The number of "unique letters" is 14 because the file contains 14 distinct letters of the alphabet from A-Z: a, b, e, h, i, l, n, o, q, r, s, t, u, and y.

Total lines = 4
Total words = 11
Total unique letters = 14 (54% of alphabet)
Average words/line = 2.8
Average word length = 3.5

Assumptions: You may assume that the input file exists and is readable.

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

Function: Write a VB 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.