Word Wrap Filter Walkthrough

In last week’s Lesson, I presented code that shows one way to wrap incoming streaming text. It’s not the easiest thing. Let me explain how last week’s code attempts to solve the problem.

Here’s a copy of the word wrap filter code from last week’s Lesson:

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

int main()
{
    char *buffer,*space;
    int c,x,right_margin,position;

    right_margin = 40;      /* make the margin variable */

    /* allocate space for the buffer */
    buffer = malloc(sizeof(char) * right_margin);
    if(buffer==NULL)
    {
        perror("Unable to allocate buffer");
        exit(1);
    }

    *buffer = '\0';         /* initialize buffer */
    space = buffer;         /* Initialize space */
    position = 0;           /*  and position */
    c = 65;                 /* Ensure that it loops */
    while(c)
    {
        c = getchar();          /* fetch character */
        *(buffer+position) = c; /* store character */
        /* if c is white space, save its location */
        if(isspace(c))
            space = buffer+position;
        position++;             /* buffer index */
        /* check to see if the margin has been reached */
        if(position > right_margin)
        {
            /* Display the buffer up to the space */
            for(x=0;x<right_margin;x++)
            {
                if( *(buffer+x)==EOF)   /* exit on EOF */
                    exit(0);
                if(buffer+x == space)   /* stop at w/s char */
                    break;
                putchar(*(buffer+x));
            }
            putchar('\n');
            /* copy the rest of the buffer to the start */
            position = 0;
            space++;
            while(space <= buffer+right_margin)
            {
                *(buffer+position) = *(space);
                position++;
                space++;
            }
            /* Re-initialize the space pointer */
            space = buffer;
        }
    }

    return(0);
}

To assist you in understanding how the code works, I’ve concocted the following video presentation. Review the video and then read my commentary to see how the code works.

The video explains how the code operates at a philosophical level. It uses the same variables names as shown in the code:

The right_margin variable is set at Line 10. You can set it to any value at which you want to wrap input. The buffer is then created by malloc() at Line 13, its size set equal to right_margin.

Line 20 initializes the buffer, setting it to a zero-length string.

Line 21 initializes the space pointer, setting it equal to the start of the buffer.

The position variable (it’s not a pointer, which is hinted at in the video) is initialized at Line 22.

Line 23 sets the value of the c variable, which ensures that the loop runs at least once; there’s always an off-chance that c could be randomly initialized to the EOF character, which would stifle the program’s output.

The if test at Line 29 is used to set the location of the space pointer, hopping it down the buffer from one white space character to another.

The right_margin test happens at Line 33.

The if test at Line 38 fixes an error that might occur when the EOF character is read before the buffer is filled. Otherwise the entire buffer is displayed.

A while loop at Line 48 copies the remaining text from the end of the buffer to the start. Again, the position variable isn’t a pointer, but it’s used with the buffer pointer to assist in copying the remaining characters.

And it all repeats until an EOF is encountered.

Now that I’ve written all that, and the code does run, I confess that it’s not perfect: Two conditions exist where this code fails to properly wrap text.

The first condition where this code fails is when a newline character is found within the text. The second condition occurs when the text contains no white space characters on which to wrap. Solutions for both situations will be presented in next week’s Lesson.

Leave a Reply