Text Formatting
October 7, 2014
Text formatting is a huge topic. Today’s exercise looks at a simple text formatter. Input to the formatter is a file of ascii text; the input is free-form, except that paragraphs are marked by blank lines (two successive newline). The formatter copies the file to its output, moving text from one line to the previous line to fill each line as much as possible. It is possible to specify the width of a line, but if none is given the width defaults to sixty characters.
Your task is to write a simple text formatter. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Well, if no one else is going to step up to the crease, here’s some C++. It uses a simple state machine to keep track of what needs to be done, in particular, in state BREAK it will insert a paragraph break before the next text output. Multiple blank lines are treated as one, blank lines at start and end are ignored. Words are read from an STL stringstream with the usual iostream operations. Input and output is just done through cin and cout. Probably best to do these things with a pipeline of filter functions of course.
Java solution. Handles spaces not perfect.
A Haskell solution. The line width argument is mandatory, not optional. It’s possible but tedious to make it optional.