I'm trying to do a program that full justifies/left and right justification a given string. I've already made the part where you know how many spaces needs to be between words in a string, but what I don't get is how to add or insert those spaces in between words.
Asked
Active
Viewed 2,338 times
1
-
Is the question about the algorithm to figure out the insertion point for the spaces, or about the particular details of how to add those spaces at the desired location? – David Rodríguez - dribeas Mar 04 '14 at 22:02
-
Please make the question more specific. "Any suggestions?" is very broad and likely to get closed. – Adrian McCarthy Mar 04 '14 at 22:11
3 Answers
0
You could
count how many words are on each line
divide the number spaces by the number of words, now we know how many spaces need to be added to each word on average
for each word in the string, word += [number of spaces]

SparkyRobinson
- 1,502
- 1
- 15
- 24
0
A simple algorithm to do a spreading is
for (int i=0; i<num_words-1; i++) {
int s0 = i * extra_spaces / (num_words - 1);
int s1 = (i + 1) * extra_spaces / (num_words - 1);
// add (s1 - s0) spaces between word[i] and word [i+1]
}

6502
- 112,025
- 15
- 165
- 265
0
Find the length of the string - sl
Count the number of space - n
Calculate the difference between l and the length of the line - ll
Calculate the width of each space - w
w=(ll-sl)/n
Print the string one word at a time, advancing by w when you have a space.

Ken Down
- 1