Skip to content

Instantly share code, notes, and snippets.

@puffin
Created February 15, 2017 14:10
Show Gist options
  • Save puffin/0d9b67a4d29c9da7a77185d45745b491 to your computer and use it in GitHub Desktop.
Save puffin/0d9b67a4d29c9da7a77185d45745b491 to your computer and use it in GitHub Desktop.
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment