String Formatter

String Formatter Example
A String in need of formatting

The String Formatter question from the 2016 AP CompSci exam asked you to implement code to format a string to a fixed width by adding spaces between words. Given a list of String and a length, you’re to create a string of that length padded with spaces.

String Formatter Starter Code

This is the code you’re given to start, with comments removed.You can see the full code, comments included, on the College Board website.

We’ve got three methods to implement. There’s also one method called leftOverSpaces that’s listed as not shown.

If you’re curious, here’s a working implementation of that method.

Part A

For part A you’re implementing the totalLetters method.  The totalLetters method should calculate the total number of letters of all strings in wordList.

This solution goes through all of the words using a for each loop and adds the length of each to the variable letand returns it. You could have also used a for or while loop, but the code’s a little shorter with a for each.

Part B

The basicGapWidth method calculates the minimum width of the gaps between words. Each gap will be at least this many spaces.

We’re given the wordList and formattedLen, which is the length of the finished string. Given those two we’re able to do the math to figure out how many spaces need to be in each gap. It’s the total number of spaces formattedLen - totalLetters(wordList) divided by the number of gaps.

Really common mistake on this part was forgetting to subtract 1 from wordList.size() since there is one fewer gap than words.

Part C

The format method of the String Formatter class uses the two methods you implemented along with leftOverSpaces to create a string of the correct length.

We know that between each word there’s going to be at least width spaces, so they’re getting appended to the string.

We’ve also got to append any additional spaces. Something that was probably a confusion point is that no gap is going to have more than one additional space added. So after adding the basic gap spaces we check to see if there are any extra spaces remaining. If there are, we add one space and then move on to the next word.

Once finished, return the string you created. In this solution I also added the trim method to the string as it’s returned. While not necessary for a correct solution, I didn’t like the idea of trailing spaces.

Why ClassCube?

Are you spending too much time grading your students' source code?

ClassCube is an online tool for Computer Science teachers that allows you to create lab assignments for your students. They'll submit their code online and ClassCube will automatically grade it for you.

More Info …

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>