I have to read in an integer which will be the length of the succeeding lines. (The lines of text will never be longer than the length provided).
I then have to read
The hardest thing about this problem is defining "as evenly as possible".
Your example:
Hello__this__is__a_test_string
... makes all the longer gaps be at the left. Wouldn't:
Hello__this_is__a_test__string
... fit the imprecise description of the problem better, with the longer gaps spread evenly through the output string?
However, let's solve it so it gives the sample answer.
numNewChars == lengthWanted minus inputString.length()numGaps -- it's the number of words minus one.n or n+1 new spaces. n is numNewChars / numGaps -- integer division; rounds down.n+1 new spaces instead of n? It's the remainder: plusOnes = numNewChars % numGapsThat's all the numbers you need. Now using whatever method you've been taught (since this is evidently a homework problem, you don't want to use language features or libraries that haven't been covered in your lessons), go through the string:
plusOnes spaces, insert n+1 spaces, in addition to the space that's already there.n spaces.One very basic method would be as follows:
String output= "";
for(int i=0; i