This is similar to the alignment used in word.


3 inputs: A 1 line string, a positive integer k, and direction (left or middle or right)

Your job is to insert new lines(\n) and spaces in the string such that every line has k characters in it and aligned to the direction given.


The words must not be split(unless their length is greater than k)

Direction can be taken as any 3 unique characters(must be mentioned).

Any format and order of input is valid(must be mentioned).

Input will only have alphabetical characters of any case(and spaces).

There will not be any trailing or leading spaces in the input and will have only 1 space between words.

Output can be either a string on one line as is in examples or multiple lines.

If the direction is left:

There should not be any spaces in left of the line and only 1 space between words.

If the direction is right:

There cannot be any spaces to the right of the line and only 1 space between words.

If the direction is middle:

There cannot be any spaces to the left or right of a line and an equal number of spaces between words on one line. In case the word length is shorter than k and only 1 word can fit in 1 line, then spaces on the right is allowed. If more than 2 words can fit in one line but they cannot be divided keeping number of spaces equal, words will shift to next line to keep number of spaces equal.

Test Cases

input: 'Hello World',6,right
output: ' Hello\n World'

input: 'Hello World',7,left
output: 'Hello  \nWorld  '

input: 'Hello World',7,middle
output: 'Hello  \nWorld  '

input: 'This is a test case',5,middle
output: 'This \nis  a\ntest \ncase '

input: 'This is a test case',5,left
output: 'This \nis a \ntest \ncase '

input: 'For example',1,left
output: 'F\no\nr\ne\nx\na\nm\np\nl\ne' #The space is removed as the next word is in a newline.

input: 'Hello Worlds How are you',5,left
output: 'Hello\nWorld\ns How\nare  \nyou  '

input: 'qwert asdfghjkl zxcvbnm',7,middle
output: 'qwert  \nasdfghj\nkl      \nzxcvbnm'

input: 'ab abcdefg',6,middle
output: 'ab    \nabcdef\ng     '

input: 'This is code golf so shortest code wins',10,middle
output: 'This    is\ncode  golf\nso        \nshortest  \ncode  wins'

input: 'Credit to Jo for this case',13,middle
output: 'Credit     to\nJo  for  this\ncase         '

This is code-golf so shortest code wins

  • \$\begingroup\$ Related. \$\endgroup\$
    – Bubbler
    Nov 14, 2018 at 7:19
  • \$\begingroup\$ Can we remove trailing whitespace? \$\endgroup\$
    – Jo King
    Nov 14, 2018 at 7:31
  • \$\begingroup\$ @JoKing, No, you can't. \$\endgroup\$ Nov 14, 2018 at 7:34
  • \$\begingroup\$ input: 'the maximums',6,middle; output: ? from the post, I could argue both 'the \nmaximu\nms ', or 'the ma\nximums'. \$\endgroup\$
    – Chas Brown
    Nov 14, 2018 at 8:28
  • \$\begingroup\$ @ChasBrown, the...\nmaximu\nms.... (same as left) \$\endgroup\$ Nov 14, 2018 at 8:31

2 Answers 2


Perl 6, 195 186 177 132 bytes

-41 bytes thanks to nwellnhof!

{->\b,\c{$!={!c*(b-$0.comb)/($0-1||1)+1};S:g{[(\S+)[\s|$]<?{b>=$0.ords&&.$!%%1}>]+|(\S**{b})}=$0.join(' 'x.$!).fmt("%{b*c||-b}s

Try it online!

Anonymous code block that takes input curried, e.g. f('text')(length, direction). Uses the values -1,0,1 as left, middle and right respectively.


Python 2, 245 243 237 225 221 bytes

import re
s=filter(bool,re.split('(\w{%d})| '%k,s))
while s:l,s=max((j,s[i:])for i in range(len(s)+1)for x in range(d&1or k)for j in[(-~x*' ').join(s[:i])]if[k,0][d|(i<2)]<=len(j)<=k);print'%*s'%(-(d|1)*k,l)

Try it online!

Takes left, middle, right as 1,0,-1


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.