Word Wrap Philosophy

Way back in the day, word wrap was fascinating. It was so unique that word processors boasted about it as a feature. I’m serious! That’s because a lot of the primitive (late 1970s and early 1980s) word processing programs did not wrap text at the end of a line. Strange, but true.

Today, word wrap is nothing to crow about. In fact, it looks puzzling not to see word wrap. In Figure 1, behold the Windows Notepad program, which features a word wrap command.

Figure 1. Windows Notepad with word wrap disabled.

Figure 1. Windows Notepad with word wrap disabled.

When word wrap is off in Notepad, each line of text extends out to some infinite right margin. That’s fine for editing those text documents where commands or information must be on a single line. For reading, however, you can activate Notepad’s word wrap feature: Choose Format > Word Wrap and the text wraps, as shown in Figure 2.

Figure 2. Windows Notepad with the word wrap feature enabled. You're still looking at a single line of text.

Figure 2. Windows Notepad with the word wrap feature enabled. You’re still looking at a single line of text.

Back in the dark days of text-only screens, the decision of whether or not to wrap text was up to the programmer, not the operating system. Your code could spew out a long line one character after the other. When the last character was put to a line of text, the cursor dropped down to the next line and continued the display. It didn’t matter if a word was split. The result was ugly.

The word wrap feature monitored text output. When the cursor reached the end of the line, the program gobbled up any word that was about to be split in two: Essentially, the code would backup and erase that word. Then a newline was output and the word was “wrapped” to the next line. On older, slower terminals you could actually see the wrapping process take place.

The wrapping process was known as rubout. It involved sending (or displaying) three characters: Backspace, space, backspace. The first backspace moved the cursor back, then space erased the existing character, then the final backspace moved the cursor back again.

Of course, wrapping text works differently whether you’re manipulating stream output or simply wrapping existing (static) text.

When text is in a stream, you have to backup and erase the last word input in order to wrap it to the next line. For extant text, you simply need to find the closest space to the wrapping point and replace that space with a newline character.

Figure 3 is my attempt to flow chart this problem, although I don’t really flow chart anything.

Figure 3. A text-wrapping flow chart. (Click to embiggen.)

Figure 3. A text-wrapping flow chart. (Click to embiggen.)

In Figure 3, you see a simple decision process:

  1. If the character index (current column position) isn’t yet at the margin, keep reading characters. Otherwise:
  2. Backup to locate the nearest white space character.
  3. Replace that whitespace character with a newline.
  4. Start the next line with the text you backed over. (This is the tricky part.)
  5. Start over.

Next week’s Lesson shows an example for wrapping existing text. If you’d like to practice on your own, here’s the skeleton I’m working with:

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

#define COLUMN 40

int main()
{
    char preamble[] = "We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defense, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America.";
    char *base,*right_margin;
    int length,width;

    length = strlen(preamble);
    base = preamble;
    width = COLUMN;

    while(*base)
    {
    }

    return(0);
}

Leave a Reply