The program given in section 1.5.4 of Kernighan and Ritchie reads a text file from the standard input stream and outputs the number of characters, words, and lines of the input file. Your task is to modify this program (or completely rewrite it) so that for each line of the input file, the characters in the line are echoed to the standard output stream preceded by the line number (formatted to take at least 2 characters, right justified, followed by a period and a space) and followed by a space and then the number of words in the line and the number of characters in the line enclosed in square brackets and separated by a comma.
After echoing the original file, output three more lines with the following information: the number of lines, words, and characters (as in the original, just more verbose); the line number of the line with the fewest number of words and the number of words on that line; the line number of the line with the fewest characters and the number of characters on that line. If two or more lines tie for the same number of words/characters, report the later line. Exactly match the formatting given in the example below.
For example, given an input file containing:
Your program must output
You may assume that the input contains only the standard ASCII printable characters (decimal codes 32 through 126) along with newline, ’\n’, and tab, ’\t’. You may also assume that the input will end with a newline character.
Hint: When a character, c, is read that is not EOF and not a newline, immediately echo it to the standard output stream with: printf("%c", c);
Attach your program file wordCount.c into the Lab 2 assignment in UNM Learn.
| [1 point]: | The program starts with a comment stating the students first and last name. |
| [1 point]: | The program is correctly named. |
| [2 points]: | Program compiles without errors or warnings on moons.cs.unm.edu using /usr/bin/gcc with the -Wall -ansi -pedantic options. |
| [5 points]: | Code follows the CS 241 Coding Standard. Review them here. |
| [16 points]: | Correct output for testWords.in. The expected output is found in testWords.out. One point is lost for each line of incorrect output. This will be graded by a diff test with testWords.out. We will also be testing your input against a suite of other inputs, so be sure to test your code against input of your own creation. |
When working on a CS machine, you can test your code with the following sequence of commands.
The second and third line fetches the “testWords” files from the course website and puts them in your current directory.
The fourth line compiles your code. The options given will cause the compiler to be a bit more verbose with its warnings than it would be by default.
The fifth line runs your code with the standard input redirected to come from the file “testWords.in” and with the standard output redirected to go to “myOut.txt”.
Finally, the last line performs a character-by-character comparison of your output with the required output. If diff returns silently (without printing anything), then you have passed all tests. If it does print something, you’ll be able to see which lines differ. Punctuation, capitalization, and whitespace all matter here!