CS 117 Assignment: Words

Assigned Wednesday, 10/5/05
Due 11:59PM Monday, 10/10/05

You may work with a partner for this assignment. No groups of three or more, please. Submit your source code via HSP.

The problem

I often want to collect some statistics on my writing. If I'm writing a proposal that has to be shorter than 2000 words, I will want to count the number of words in each of my drafts. If I'm trying to write software documentation at a sixth-grade reading level (tragically, that's what the software experts usually recommend), I'll want to keep an eye on the number of letters and syllables in my words, the number of words in my sentences, and the number of sentences in my paragraphs.

Actually, I just like fooling around with this kind of thing. It pleases me to know how many words there are in Middlemarch, or how many times the word "whilst" appears in Hamlet (326398 and 4, respectively).

For this assignment, you will write a program that takes a text file as input and reports:

A little more detail

When you compile your program (javac Words.java or something similar) and run it like so:

java Words somefile.txt

your program should produce an easy-to-read report that looks something like this:

Number of words: 1729
Average word length: 5.72 letters
Longest word: electroencephalography (22 letters)
Shortest word: a (1 letter)
Occurrences of "the": 62
Alphabetically latest word: zygomorphic

A small offering

Probably the hardest part of writing this program is reading one word at a time out of the input file. Punctuation, extra spaces, ends of lines, and the end of the file itself can all cause trouble if you aren't careful. With this in mind, I have written a Java program WordCounter.java to help you out.

Start early, have fun, and bring questions to class.