Programming Problem #10
"Text Justification"

One of the most important functions of a word processor is text justification, which means formatting paragraphs so that lines are as full as possible without breaking any words. In this problem you will write a simple text justifier.

The input consists of one or more paragraphs. Each paragraph begins with a line containing the target width W (0 < W <= 80) in the first column. This is followed by one or more lines of text that are terminated by the next paragraph or the end of the file. No input line will be longer than 80 characters. Paragraph text may contain numbers, but they will never begin in the first column of an input line (although they may end up in the first column of an output line).

Each paragraph must be displayed according to the following rules. A "word" is any consecutive sequence of nonblank characters.

  1. Each line must be left-justified and as long as possible.
  2. No line may be longer than W characters.
  3. Adjacent words in the output must be separated by exactly one space.
  4. Words may be broken only at a hyphen, as shown in the example below.
  5. Successive paragraphs must be separated by one blank line.
Input must be read from the file "prob10.in", and output must be written to the file "prob10.out". All output to the screen will be ignored.

Example Input

<BOF>
35
One   characteristic   of the
           Von Neumann
architecture is   one-word-at-a-time   execution.
15
"Cry 'Havoc!',

and let slip the dogs of war."
<EOF>

Example Output

One characteristic of the Von
Neumann architecture is one-word-
at-a-time execution.

"Cry 'Havoc!',
and let slip
the dogs of
war."

Dr. Eric Shade