next up previous
Next: and the day Up: Inaugural UTICPC Previous: Prime numbers

Text formatting

Not too long ago, CDF did not have a laser printer. Instead, we printed programs and manual pages on a line printer. Each line of text -- sorry, no graphics -- would be mechanically stamped in a monospace font (each character being the same width). It was hard to make our output less ugly.

One of the ways to beautify text in such an environment is to justify text on both sides. That is, in the body of a paragraph, the first letter of the first word on each line should appear in the first column, and the last letter of the last word on each line should appear in the last column. This is achieved by inserting spaces between words as evenly as possible. To make these spaces not too wide, we pack as many words as possible on each line, and then insert the spaces to fill it out to the correct width. (More on this below.)

However, there should always be a 5 space indentation for the first line of a paragraph. That is, the first character of the first word of a paragraph should appear in position 6. Also, the last line of a paragraph should be justified only on the left side, i.e. spaces should not be inserted to fill it out to the right.

You will write a simple formatter to do this job for a single paragraph.

Input: The input to your program is a positive integer c (with 10<c<75), specifying the width of the margins in characters, followed by a sequence of one or more words that together make up one paragraph. Each word consists of 1 to c consecutive printable (ASCII code 33 through 126) characters. Words are separated by blanks, tabs, or newlines.

Output: Your program should format the paragraph in the manner described above.

The first printing character on every line (except the first) should appear in the first column.

It will always be possible to format a line of text without overflowing the right margin. In particular, the first word will be at most c-5 characters long.

If e extra spaces need to be inserted between words to fill out a line, then they should be distributed in a round-robin fashion. Specifically, if there are g gaps to be filled, then the first of the e spaces should be given to the first gap, the second to the second gap, and so on. If we reach the last gap and still have more spaces to give out, then we go back to the first gap, and repeat. If only one word fits on a particular line, then it should be left-justified, and immediately followed by a single newline character.

Do not print any empty lines before or after the paragraph. The last word in each line (including the last line) should be immediately followed by a newline character.

Sample input 1:

60
        Not too long ago, CDF did not have a laser printer.  
Instead, we printed
programs and manual pages on a line printer.  Each line of text 
 --- sorry, no graphics --- 
would 
be mechanically stamped in a monospace font (each
character being the same width).  It was hard to make our output less ugly.

Corresponding sample output:

     Not  too  long  ago,  CDF did not have a laser printer.
Instead,  we  printed  programs  and  manual pages on a line
printer.  Each line of text --- sorry, no graphics --- would
be  mechanically stamped in a monospace font (each character
being  the  same width). It was hard to make our output less
ugly.

Sample input 2:

22

I once had a friend who owned a VIC-20 computer.

It only had   5KB of RAM, and so only 512 bytes were allocated for screen
 memory.  Because each screen position occupied a byte,
the default screen was a 22 by 23 character grid.

Ick!

Corresponding sample output:

     I   once   had  a
friend   who  owned  a
VIC-20   computer.  It
only  had  5KB of RAM,
and  so only 512 bytes
were   allocated   for
screen memory. Because
each  screen  position
occupied  a  byte, the
default  screen  was a
22   by  23  character
grid. Ick!


next up previous
Next: and the day Up: Inaugural UTICPC Previous: Prime numbers

David Neto
Thu Jan 9 19:12:26 EST 1997