In: Computer Science
Text Wrap Problem
Write a program in Python that takes an input string and prints it as multiple lines of text such that no line of text is greater than 13 characters and words are kept whole.
For example, the first line of the Gettysburg address:
Four score and seven years ago our fathers brought forth upon this
continent a new nation, conceived in liberty and dedicated to the
proposition that all men are created equal
Becomes:
Four score
and seven
years ago our
fathers
broughtforth
upon
this
continent a
new nation,
conceived in
liberty and
dedicated to
the
proposition
that all men
are created
equal
Source Code:
text=input().split(' ')
i=0
while(i<len(text)):
temp=text[i]
while(len(temp)<=13 and i+1<len(text)):
if((len(temp)+1+len(text[i+1]))<=13):
temp=temp+"
"+text[i+1]
i=i+1
else:
break
print(temp)
i=i+1
Sample input and output: