help on a text formatting program

I need to design a function that, given an open input stream and a value L, reads a plain-text document from that stream and typesets each paragraph so each line begins with a non-space character, each pair of successive words in a line are separated by a single blank, none of the lines contain more than L characters, and all the lines except the last one in the paragraph are so nearly full that adding another blank plus the first word of the next line would have taken use over the L character max.
Paragraphs in the input and output will be separated by empty lines, and, for the purpose of this assignment, a "word" is defined as any sequence of characters not containing a blank or a line terminator. For example, in the preceding sentence, "terminator.", including the period, would count as a word.
For example, given a L==20 and an input stream containing the text:

'Mine is a long and a sad tale!' said the Mouse, turning to Alice,
and sighing.

'It IS a long tail, certainly,' said Alice, looking down with
wonder at the Mouse's tail; 'but why do you call it sad?'
the output would be

'Mine is a long and
a sad tale!' said
the Mouse, turning
to Alice, and
sighing.

'It IS a long tail,
certainly,' said
Alice, looking down
with wonder at the
Mouse's tail; 'but
why do you call it
sad?'


Any suggestion is welcome. I am a newbie and got totally lost...
Topic archived. No new replies allowed.