Previous: 4.2.3 Counting Words
Up: 4.2 Sample Character Processing Functions
Previous Page: 4.2.3 Counting Words
The final task in this section extends the word count program to print each word in the input stream of characters.
WDS: Read characters until end of file and keep a count of characters, lines, and words. Also, print each word in the input on a separate line.
The logic is very similar to that of the previous program, except that a character is printed if it is in a word, i.e. if inword is True. We will decide whether to print a character only after a possible state change of inword has taken place. That way when inword changes from False to True (the first character of a word has been found) the character is printed. When inword changes from from True to False (a delimiter has been found ending the word) it is not printed, instead we print a newline because each word is to be printed on a new line. So our algorithm is:
initialize counts to zero, set inword to False while the character read, ch, is not EOF increment character count, chrs if ch is a newline increment line count, lns if NOT inword AND ch is NOT delimiter increment word count, wds set inword to True else if inword and ch is delimiter set inword to False print a newline if inword print ch print resultsand the code is shown in Figure 4.16. This code was generated by simply copying the file cnt.c and making the necessary changes as indicated in the algorithm.
The program file is compiled and linked with chrutil.c. and the following sample session is produced.
Sample Session:
In this section we have seen several sample programs for processing characters as well as some new programming techniques, in particular, splitting the source code for a program into files of related functions with separate compilation of each source code file. The executable program is then generated by linking the necessary object files. In the next section, we turn our attention to several new control constructs useful in character processing as well as in numeric programs.