Question: CS231 Lab #4 For this assignment, write a short program which reads one or more sentences from standard inpui-that is, using the cin input stream
CS231 Lab #4 For this assignment, write a short program which reads one or more sentences from standard inpui-that is, using the cin input stream -and calculates the number of words, the average word length, and the maximum word length (the length of the longest word), and outputs these statistics to the screen as shown below. Your program should use a while loop to read from cin, one character at a time, until there is no more input to process. For simplicity, assume that sentences are always terminated by the symbols ". '?", and that the symbols that separate words from one another within a sentence are a '(a blank space), -',, or", . These symbols should not be included in the length of the words, nor should they be counted as words. (Hint: I suggest using a switch statement to compare each character read from cin with the symbols listed here. Label one case action with a set of case labels, one label for each symbol; see the "Computation: Part 1" lecture notes for an example of how to do this.) First, test your program using a single sentence, entered at the keyboard on one line, as input. Don't forget to type CTRL+Z at the end of your input, or CTRI+D on Macintosh or Linux systems. Example: To be or not to be; that is the question! The output for this sentence should resemble the following: Number of Words: 10 Average Word Length: 3 Maximum Word Length: 8 After you have this working (and don't move on to this next step until you do), use piping at the command line to feed a text file into your program as input. As we have discussed in class, one of the advantages of the C++ streaming model for I/O is that it is possible to redirect the output of one program into the input of another. The program on the "input" end of the pipe will then be able to read this output from cin, as if it was entered at the keyboard, without any modifications to the code. Attached is a sample input file, a chapter from Mark Twain's "A Connecticut Yankee in King Arthur's Court." Remember to change your working directory to the directory (folder) containing the compiled version of your program (use the CD command), and to copy the input file into this same directory. On Windows workstations, you can use the TYPE command-line utility to pipe the contents of the input file into your program. For example, assuming that the compiled version of your program is named Lab04.exe
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
