The Reversing Words Filter – Solution

The task for this month’s C programming Exercise is to code a filter that reverses single words. As a filter, the code deals with standard input and output, but it must store this input in word-sized chunks for the reversing process to work.

I asked Google how long the longest word is in the English language. It replied that pneumonoultramicroscopicsilicovolcanoconiosis is 45 letters long. So for a word-reversing filter to work, I need a buffer that holds at least 46 characters (pneumonoultramicroscopicsilicovolcanoconiosis plus the null character). Other human languages may require a larger buffer, but the technique works the same.

Here is my solution:

2025_09-Exercise.c

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

int main()
{
    /* the longest legitimate word in English
       is about 45 characters */
    const int longest = 64;
    int ch,offset;
    char word[longest];

    offset = 0;
    while( (ch=getc(stdin)) != EOF )
    {
        /* store alphabetic words */
        if( isalnum(ch) || ch=='\'' )
        {
            word[offset] = ch;
            offset++;
            /* check for overflow */
            if( offset==longest )
            {
                fprintf(stderr,"\nInput overflow\n");
                return 1;
            }
        }
        else
        {
            /* if the buffer has content, output
               it backwards */
            while(offset>0)
            {
                offset--;
                putc(word[offset],stdout);
            }
            /* output the non-alpha character */
            putc(ch,stdout);
        }
    }

    return 0;
}

Constant int variable longest sets the size of the buffer, with more than enough room for pneumonoultramicroscopicsilicovolcanoconiosis. Integer variable ch gobbles input. Variable offset tracks word length. Buffer word[] holds words as they’re input.

The while loop fetches standard input until the EOF (end-of-file) is encountered, which is a typical construction for a filter. The loop’s if-else structure tracks words input, storage, and output.

The if condition uses the isalnum() ctype function to test for alphanumeric characters. The || (OR) test checks for the apostrophe to catch contractions and possessives. When the if condition is true, the character input is stored in the word[] buffer. Variable offset is incremented. A nested if test checks for overflow just in case. If so, the program terminates.

The else part of the construction deals with non-alphanumeric character input. First, a while loop empties any text stored in the word[] buffer. The looping condition is offset>0, so this loop is skipped when the buffer is empty. Otherwise, text in word[] is output one character at a time, using variable offset to work backwards through the buffer.

Whether the while loop outputs a backwards word or not, the next statement outputs the non-alpha/apostrophe character caught by the else condition. Variable offset is equal to zero at this point, which primes the next spin of the main while loop to potentially store the next word.

Here’s a sample run:

Here's a sample run:
s'ereH a elpmas nur:

And, of course:

pneumonoultramicroscopicsilicovolcanoconiosis
sisoinoconaclovociliscipocsorcimartluonomuenp

Finally:

The word pneumonoultramicroscopicsilicovolcanoconiosis is a long word
ehT drow sisoinoconaclovociliscipocsorcimartluonomuenp si a gnol drow

This code took me a few tries to get perfect. One of my drafts output null characters after each reversed word. This output was related to the value of variable offset being decremented after the character was output. Null characters don’t appear on the screen. To ensure that your code doesn’t output null characters, run it through a program like hexdump and watch for those little 00 bytes.

I hope that your solution met with success!

Leave a Reply