You may work with a partner for this assignment. No groups of three or more, please.
When you are done with this assignment, please submit it in a source file named words.p using the Homework Submission Program.
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)
If there is more than one different longest word or shortest word, you should report the first one to appear in the input file.
{==================================================== ReadWord reads the next "word" from standard input (i.e. the keyboard, or the input file specified by "< file" at the Unix command line), returning it via the variable parameter "word". Here, a word consists of a block of contiguous letters and/or apostrophes. Note that ReadWord will read past eoln when it is searching for the next word. We'll discuss this rather complicated code in class sometime in the next few weeks. ====================================================} procedure ReadWord( var word : string ); const apostrophe = chr(39); {The apostrophe character has ASCII value 39} type stateType = (beforeWord,duringWord,done); {See pages 198-200 of Abernethy & Allen to see what this is about.} var ch : char; state : stateType; function IsAlpha( c : char ) : boolean; begin IsAlpha := ((c >= 'a') and (c <= 'z')) or ((c >= 'A') and (c <= 'Z')); end; begin word := ''; state := beforeWord; while state <> done do begin if eof then state := done else if eoln then begin if state = beforeWord then readln else state := done end else begin read( ch ); if (ch = apostrophe) or (IsAlpha(ch)) then begin word := word + ch; state := duringWord end else if state = duringWord then state := done end end end; {end of ReadWord}
Start early, keep in touch, and
have fun.