600.120 Intermediate Programming
Spring 2010 -- Lab 6

The Problem:

Write a program which will create a right and left justified version of some input text. The program should take a command-line argument which is the number of characters desired in each output line of text. Then read from standard input and write to standard output, using input redirection in unix to work with plain text files. The program must reformat the input when it is output, so that there are exactly the specified number of characters per line, in a right and left justified manner, by padding between words with extra spaces. Do not split any words.

For this input:

Here is some sample text that will be
input for the program.  When the program is run it will
re-wrap the words and display them with right and left margins
justified.
The length of each line is given by a command-line argument.

and line size 30, here is the output:

Here  is some sample text that 
will be input for the program.
When  the  program  is  run it 
will  re-wrap  the  words  and 
display  them  with  right and 
left  margins  justified.  The 
length  of  each line is given 
by  a  command-line  argument.

For line size 15, here is the output:

Here   is  some
sample     text 
that   will  be 
input  for  the 
program.   When 
the  program is 
run   it   will 
re-wrap     the 
words       and
display    them 
with  right and 
left    margins
justified.  The 
length  of each
line  is  given
by            a 
command-line 
argument.         (there's not much you can do if there's only 1 word that fits)

Implementation Details:

You must use proper paired programming technique (one driver/one navigator at a time) to create a solution. Work with whoever is sitting next to you. (At most one 3-person group per lab section if there are an odd number of people.)

You are expected to use the STL for most of the data storage and processing (string, vector, list). Do not store any more data than is necessary. (Ie, process as you read, which will look odd if running the program without input redirection.)

You must have a function that reads a line of input and returns it through a parameter as a container (vector or list) of words (including any attached punctuation). The input source must be passed as a parameter also. The function must return the updated input stream.

You must have a function that has three parameters: an output stream, a container of words, and an integer line length. It should write as many words as possible to the output stream right & left justified for the line length. The word container should be updated so that it only includes the words that did not get written. The function should also return the updated output stream.

You must use const to protect data as much as possible.

You can only use specific "using" statements, not "using namespace std".

You do not have to use multiple files, but you must use multiple functions to organize the program.

Practice incremental coding! Write one of those functions at a time and test thoroughly before attempting the next.