split line of text

Posted by plys on Stack Overflow See other posts from Stack Overflow or by plys
Published on 2010-05-15T07:30:00Z Indexed on 2010/05/15 7:34 UTC
Read the original article Hit count: 293

Filed under:
|
|

Hi all,

I was wondering if there is an algorithm to split a line into multiple lines, so that the resulting set of multiple lines fit into a square shape rather than a rectangle. Let me give some examples,

Input: Hi this is a really long line.

Output:
Hi this is
a really
long line

Input: a b c d e f

Output:
a b c
d e f

Input: This is really such looooooooooooooooooooong line.This is the end.

Output:
This is really such
looooooooooooooooooooong
line This is the end.

If you see in the above examples, input line fits into a wide rectangle. But the output more or less fits into a square shape.

Essentially what needs to be done here is simply count the number of characters in the line, take the square root of that number. Then put square root number of characters in each line. But in the above example, the splitting needs to be done by respecting word wraps instead of characters. Is there any standard algorithm for this? Any code examples/ pointers would be appreciated!

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about line