AP CSA FRQ Guide
2016 FRQ 4: StringFormatter
Calculate word lengths and gap sizes, then format a list of words into a line with distributed spaces.
Skills Tested
What this FRQ is really practicing
Review these topics before attempting the problem.
Starter Approach
How to begin with your own plan
- 1Sum word lengths.
- 2Compute base spaces between words using integer division and the required totalLetters helper.
- 3Build the formatted result using basicGapWidth and leftoverSpaces, distributing leftover spaces from left to right.
Attempt the problem before opening the solution.
Common Mistakes
Mistakes to watch for while writing
Self Check
Review questions before you submit
Sums the length of every word.
Use word.length() for each item in the list.
Divides extra spaces by the number of gaps.
There is one fewer gap than there are words.
Builds and returns a formatted String.
Repeated concatenation or a StringBuilder can work.
Handles leftover spaces beyond the basic gap width.
The earliest gaps receive the extra spaces.
Practice Links
