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 in each line of text and convert the spaces to an underscore as evenly as possible. For example:
I would enter the line length of 30. Then a line of text Hello this is a test string
. Then all of the spaces will be converted to underscores and padded out so that the text fills the given line length like so: Hello__this__is__a_test_string
. As you can see, the original text had a length of 27 characters, so to pad it out to 30 characters I had to add 3 extra spaces to the original text and then convert those spaces to the underscore character.
Please can you advise a way that I can go about this?
I had to do something similar to this in Java recently. The code itself is relatively straightforward. What I found took the longest, was getting my head around the justification process.
I started by making a step by step process of how I would justify text manually.
Doing this made coding the algorithm much simpler for me!
Finding out how long the line and the string on said line are
You said you have read in the line length and the text on the line so 1 and 2 you have already done. With 2 being a simple
string.length()
call.Calculating the number of spaces required to add to the string to equal the line length is simply taking the line length and subtracting the length of the string.
Finding out how many gaps there are between all the words in the string
There is probably more than one way of doing this. I found that the easiest way of doing this was converting the string into a char[] and then iterating through the characters checking for ' ' and setting a count for when it does find a ' '
Calculating how many spaces to add to each gap
This is a simple division calculation!
Note: You have to make sure you're doing this division with integers! As 5 / 2 = 2.5, therefore you KNOW you have to add 2 spaces to each gap between the words, and divisions using int's truncates the decimal number to form an integer.
Add the result to each gap
Before being able to add the number of strings required to add to each gap, you need to convert this number into a string of spaces. So you need to write a method for converting a given integer into a string of spaces equating to that given number. Again, this can be done in different ways. The way I did it was something like this
The way I did this was to convert the string into an array of substrings, with the substrings being each word in the array. If you look up the String class in the javadoc you should find the methods in the String class you can use to achieve this!
When you have your array of substrings, you can then add the string of spaces to the end of each substring to form your new substring!
Calculating how many extra spaces there are extra
This is again a simple calculation. Using the % operator you can do a remainder division similar to the division we did earlier.
The result of this calculation gives us the number of extra spaces required to justify the text.
Add the extra spaces serially to each gap
This is probably the most difficult part of the algorithm, as you have to work out a way of iterating through each gap between the words and add an extra space until there are no more extra spaces left to add!
Return string
Let's try to break the problem down:
Subtract the length of the string from 30 - that's the number of extra spaces you'll be adding somewhere (3 in this case).
Count the number of existing spaces (5 in this case).
Now you know that you need to distribute that first number of extra spaces into the existing spaces as evenly as possible (in this case, distribute 3 into 5).
Think about how you would distribute something like this in real life, say balls into buckets. You would probably rotate through your buckets, dropping a ball in each one until you ran out. So consider how you might achieve this in your java code (hint: look at the different kinds of loops).
You just need to call
fullJustify()
method where in list of words needs to be passed along with the max width of each line you want in output.Below is the sample conversion where maxWidth was 80 characters: The following paragraph contains 115 words exactly and it took 55 ms to write the converted text to external file.
I've tested this code for a paragraph of about 70k+ words and it took approx 400 ms to write the converted text to a file.
Input
Output