When you are done with this assignment, please submit it using the Homework Submission Program.
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, by the way).
For this assignment, you will write a program that takes a text file as input and reports
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
////////////////////////////////////////////////////////////////// // // GetWord retrieves the next word from standard input (cin), // returning true if there is a word remaining in the input, // and false if not. In the former case, the word is // returned via the reference parameter "word". // // GetWord defines a "word" to be any contiguous block of // letters, hyphens, and apostrophes that starts with a // letter. Thus, "don't" and "hocus-pocus" are both // considered words (as they should be), but so is // "Q--'--". Defining "word" better than this is // possible, but complicated. // // We'll talk about how GetWord works some time in class. // ////////////////////////////////////////////////////////////////// bool GetWord( string& word ) { char c; // Scan through the text until we find a letter. while( cin.get(c) && !isalpha(c) ) { // This loop body is empty on purpose. No, really. } // If we reached the end of the file without finding // a word, return false. if( !cin ) return( false ); // Otherwise, build the word one character at a time // and return true. word = c; while( cin.get(c) && (isalpha(c) || c == '-' || c == '\'') ) { word = word + c; } return( true ); }
Start early, keep in touch, and
have fun.