String Formatter Free Response Solution

The String Formatter free response 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.

public class StringFormatter {
    public static int totalLetters( List<String> wordList ) {
        /* Part A */
    }
    
    public static int basicGapWidth( List<String> wordList, int formattedLen ) {
        /* Part B */
    }

    public static int leftoverSpaces( List<String> wordList, int formattedLen ) {
        /* Implementation not shown */
    }

    public static String format( List<String> wordList, int formattedLen ) {
        /* Part C */
    }
}

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. I needed it to build unit tests for my students.

public static int leftoverSpaces( List<String> wordList, int formattedLen ) {
    /* Implementation not shown */
    return formattedLen - totalLetters( wordList ) - ( basicGapWidth( wordList, formattedLen ) * wordList.size() - 1 );
}

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.

public static int totalLetters( List<String> wordList ) {
    /* Part A */
    int let = 0;
    for ( String word : wordList ) {
        let += word.length();
    }
    return let;
}

This solution goes through all of the words using a for each loop and adds the length of each to the variable let and 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.

public static int basicGapWidth( List<String> wordList, int formattedLen ) {
    /* Part B */
    return ( formattedLen - totalLetters( wordList ) ) / ( wordList.size() - 1 );
}

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.

public static String format( List<String> wordList, int formattedLen ) {
    /* Part C */
    int width = basicGapWidth( wordList, formattedLen );
    int leftoverRemaining = leftoverSpaces( wordList, formattedLen );

    String formatted = "";
    for ( int i = 0; i < wordList.size() - 1; i++ ) {
        formatted += wordList.get( i );

        for ( int s = 1; s <= width; s++ ) {
            formatted += " ";
        }

        if ( leftoverRemaining > 0 ) {
            formatted += " ";
            leftoverRemaining--;
        }
    }

    formatted += wordList.get( wordList.size() - 1 );

    return formatted.trim();
}

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.

This site contains affiliate links. If you click an affiliate link and make a purchase we may get a small commission. It doesn't affect the price you pay, but it is something we must disclose.