[previous_group] * [up] [next] [next_group]

Extracting Words from Text

Task:

Write a program that reads text from a file and keeps a count of the number of characters, lines, and words in the file. In addition, print each word in the input on a separate line.

The Algorithm

This program is similar to the counting programs we have done before in count3.c. To count words we need to add steps to detect words by finding the beginning and end of a word.

           initialize counts to zreo
           at the beginning, we are not in a word
           
           get the first character
           while there are more characters
                increment the character count
                if the character is a newline
                     increment the line count
                if we are not in a word, and the character is not a delimeter
                (we have just found the beginning of the word)
                     increment the word count
                     remember we are now in a word
                otherwise, if we are already in a word and the character is a delimeter
                (we have just gone beyond the end of the word)
                     remember we are not in a word
                     print a newline
                if we are in a word
                     print the current character
                get the next character
           print count results

The code is in wds.c. In this program we use macros and functions in chrutil.h and chrutil.c.

[next]