Sketching the solution
(find the bugs!)
n = 0 /* chars written on current line */
while (we can read a token) {
if (the token is a paragraph break) {
start a new output paragraph
} else {
lw = length(word)
if (n+lw > max) {
start a new line; n = 0
}
write the word; n += lw
}
}