One Word at a Time

A filter is a command line tool that takes input, modifies it, and generates output. This month’s Exercise is to create such a filter, one that takes text input and spews it out one word at a time.

Don’t fret! I’m not asking you to create a word wrap filter, which was mulled over during most of May’s Lessons. Instead, this month’s Exercise is to create a filter that reads standard input up until a white space character is encountered. At that point, the word is displayed on a line by itself.

For example, for the input We the People of the United States the code would generate the following output:

We
the
People
of
the
United
States

Because I’m feeling generous, my simple solution to this problem is presented below.

#include <stdio.h>
#include <ctype.h>

#define SIZE 80

int main()
{
    char buffer[SIZE];
    int index,c;

    /* initalize variables */
    c = 1;                          /* avoid random EOF */
    index = 0;
    buffer[index] = '\0';

    /* process standard input */
    while(c != EOF)
    {
        c = getchar();
        if( isspace(c) )            /* whitespace found */
        {
            buffer[index] = '\0';   /* cap the string */
            puts(buffer);           /* display w/newline */
            index = 0;              /* reset index */
        }
        else                        /* keep filling the buffer */
        {
            buffer[index] = c;
            index++;
        }
    }

    return(0);
}

Don’t think you’re getting off easy this month, however. That’s because my simple solution overlooks two distinct problems. If you can already see the problems, then good for you!

The first problem is an issue I call bounds checking, although the specific term applied to the sample code would be buffer overflow. Therefore your first task is to re-write the code so that the buffer[] doesn’t keep filling up with characters after its SIZE is encountered.

And, no, the solution is not just to make the SIZE constant larger.

The second problem involves a situation where perhaps more than one white space is encountered in a row. For example, the input This is   an  oddly    formatted  sentence still needs to be output as

This
is
an
oddly
formatted
sentence

And not:

This
is

an


oddly

formatted

sentence

Fix with my prototype code (above), or craft new code, so that these two situations are avoided.

Please eschew your desire to peek ahead at my solution before you attempt to code your own. Further, if you’re feeling up to it, consider coding a solution by using pointers. Heck, toss in a malloc() function in there. Give yourself a challenge!

Remember: My solutions demonstrate only one way to solve the problem. In programming, many approaches are possible, some can be more elegant or cryptic, but no one correct answer exists.

Exercise Solution

Exercise Solution with Pointers

Leave a Reply