P
P
psldvch2021-11-21 12:38:27
Java
psldvch, 2021-11-21 12:38:27

How to add spaces between words so that the lines become the same length?

There is a poem, represented as an array of strings.
It is necessary to distribute spaces between words so that all lines are the same length.

I will give an example:

"At the seaside, there is a green oak,"
"A golden chain on that oak."
"Day and night, the cat is a scientist"
"Everything goes around the chain."
"He goes to the right - the song starts,"
"To the left - he tells a fairy tale."

And you need to do this:

619a13e88dd4b019205585.png

How can I do this?
Maybe there is a special method, or using regular expressions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ronald McDonald, 2021-11-21
@Zoominger

1. Find the string with the maximum length. This will be the maximum line width;
2. Count the spaces in each line;
3. Find for each line the difference between its length and the length of the big line;
4. Divide the difference by the number of spaces with a remainder;
5. Multiply the spaces by the integer part (add X-1 spaces to each space);
6. Scatter the rest between the remaining spaces.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question