Skip to content

Latest commit

 

History

History

177-justify-the-text

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Justify the Text

Challenge Description:

Write a program that reformats the text into lines of 80 symbols by stretching the text to full line width by adding extra spaces.

Longer series of spaces should go first. For example:

  • if you need to add just one extra space, add it between the first and the second word
  • if there are 4 words in a line, and you need 10 spaces to stretch the text up to 80 symbols, add 4 spaces between the first and the second word, 3 spaces between the second and the third word, and 3 spaces between the third and the fourth word.

The last line of the paragraph remains unchanged.

Input sample:

The first argument is a filename. The input file contains a text.

For example:

Hello, World!
The precise 50-digits value of Pi is 3.14159265358979323846264338327950288419716939937510.
But he who would be a great man ought to regard, not himself or his interests, but what is just, whether the just act be his own or that of another. Next as to habitations. Such is the tradition.

Output sample:

Print to stdout the text, reformatted into lines of 80 symbols by stretching the text to the full line width by adding extra spaces.

For example:

Hello, World!
The         precise         50-digits        value        of        Pi        is
3.14159265358979323846264338327950288419716939937510.
But  he  who would be a great man ought to regard, not himself or his interests,
but what is just, whether the just act be his own or that of another. Next as to
habitations. Such is the tradition.