AP CSA FRQ Guide
2017 FRQ 3: Phrase
Modify a Phrase class by replacing a specific occurrence of a target string and finding the last occurrence of a target.
String Search and Replacement22 minute targetStringindexOfsubstringloopsedge cases
Skills Tested
What this FRQ is really practicing
StringindexOfsubstringloopsedge cases
Review these topics before attempting the problem.
Starter Approach
How to begin with your own plan
- 1Part A: locate the nth occurrence using repeated searching and update the phrase only when it exists.
- 2Part B: search until no later occurrence exists, then return the last starting index or -1.
Attempt the problem before opening the solution.
Common Mistakes
Mistakes to watch for while writing
Replacing the first occurrence instead of the nth occurrence.
Continuing the next search from the same index and finding the same occurrence again.
Changing the phrase when the nth occurrence does not exist.
Self Check
Review questions before you submit
Uses indexOf to locate target occurrences.
Repeated indexOf calls are the cleanest approach.
Tracks which occurrence has been found.
The nth occurrence is not always the first occurrence.
Rebuilds the phrase using substring pieces.
Strings are immutable, so replacement must create a new string.
Handles missing occurrences safely.
indexOf returns -1 when the target is not found.
Practice Links
