Starting from:
$29.99

$23.99

A text formatter Program #1 Solution

A text formatter is a program that arranges text according to rules specified by the user of the formatter. A web browser acts as a text formatter, as does a word processing tool like Microsoft Word. For this assignment you will implement a small amount of text formatting functionality. You will read in text and reformat it according to instructions given to your program.

 

The input to your program consists of lines of text. The text consists of a sequence of words (characters) separated by whitespace (spaces, tabs, newlines). The output of your program is a reformatting of the input according to specified rules.

 

The output of your program consists of lines of text, almost all of which should have the same length. For example, if the input of your program is as follows:

Screen door slams. Mary’s dress

sways. Like a     vision she dances

across the porch as the radio      plays.

 

If you are asked to generate 40 character lines, the output would be:

 

Screen  door  slams. Mary’s dress sways. Like a vision she dances across the porch as the radio plays.

 

Observe the following from this simple example:

•     Words on the input might be delimited by more than one instance of whitespace. The program treats multiple spaces on input as a single space.

•     In order to make the output lines fit the proper length, extra spaces may need to be added between words on output. The extra spaces are added in multiple places.

•   The last line is less than the specified number of characters, because there are no more words left.

 

Some additional specifications:

•     One or more blank lines is taken to mean “end of paragraph”; when it reads a blank line, your program should end the current paragraph and skip a line. Several consecutive blank lines should be treated as a single blank line.

•     There may be a circumstance where it is impossible to generate a line less than the specified length (for example, suppose that the line is of length 10 and a word is of length 15). In those cases, your program should insert a dash character to break the long word into two pieces, and should put the second piece at the beginning of the next line.

•     A line in the form “.ll N”, where N is a positive integer, causes a new paragraph to begin. All subsequent lines will be of length N. If N is not a positive integer that is less than 120, the line is ignored.

•   Any other line that begins with a dot is ignored

 

Your program will take a single command line argument, which is the name of the input file to read. The output should be written to standard out.

More products