Write a program that reads words and arranges them in a paragraph so that all
other than the last one are exactly forty characters long. Add spaces between words to make
the last word extend to the margin. Distribute the spaces evenly. Use a helper function for
that purpose. A typical example would be
Four score and seven years ago our
fathers brought forth on this continent
a new nation, conceived in liberty, and
dedicated to the proposition that all
men are created equal.
- Read the entire paragraph into a string
- Parse the string into a vector of strings, each string containing one word
- construct a line of text by appending the words one by one. Before appending each word, check whether adding it would make the length of the line more than 40 characters.
--- if it does, then:
----- add the right number of spaces to make the line 40 characters long
----- begin a new line with the word
--- if it does not, then:
-----append it to the existing line
Read the text one word at a time. If the word can be added to the current line, add to a vector that represents the words on the line. If the word can't be addd to the existing line then calculate the required spacing to align the line properly, display the line from the vector and clear the vector.
A simple implementation could be (not fully tested for edge conditions):
Four score and seven years ago our
fathers brought forth on this continent
a new nation, conceived in liberty, and
dedicated to the proposition that all
men are created equal.