Building strings — and summing values — recursively.
Same pattern, two return types: concatenate strings to build, or add ints to accumulate. The structure is identical.
Recursion on Strings shrinks the input by removing or processing one character per call. The recursive case typically uses substring to peel off a piece; the base case returns an empty String "" or a final character.
Summation works the same way: process one element, then add to the result of the recursive call on the rest.
On the AP exam, String recursion appears in reverse, repeat, and palindrome FRQs; summation appears in digit-sum and array-sum problems.
Process one piece; recurse on the rest; combine.
Concatenate for strings; add for sums.
Reverse a String
public String reverseString(String s) {
if (s.length() <= 1) return s;
return reverseString(s.substring(1)) + s.charAt(0);
}
Peels off the first char; recurses on the rest; appends the peeled char at the end. The last char of the result is the first char of the input.
Repeat a String n times
public String repeat(String s, int n) {
if (n <= 0) return "";
return s + repeat(s, n - 1);
}
Each call concatenates one copy of s; n counts down. repeat("ab", 3) returns "ababab".
Sum the digits of a number
public int sumDigits(int n) {
if (n == 0) return 0;
return (n % 10) + sumDigits(n / 10);
}
Peels off the last digit; recurses on the rest of the number; adds the digit to the smaller sum.
The shared pattern
- Base case: empty or zero — returns the additive identity (
""or0). - Recursive case: isolate one piece (first char, last digit) and combine with the recursive call.
- Direction: input shrinks by one piece each call.
String and summation vocabulary.
Used in building and accumulating problems.
- Concatenation — joining strings with
+. - Substring — String method extracting a portion.
- charAt — accessing one character.
- Accumulation — building a running result.
- Identity — empty String for concat; 0 for sum.
- Peel — isolating one piece per call.
How recursive building is tested.
Substring boundaries and concatenation order dominate.
- Base case empty.
""for strings,0for sums. - Concatenation order matters.
recursive() + s.charAt(0)reverses; the opposite preserves. - substring(1) peels off the first char.
- charAt(0) gets the first char.
MCQ patterns: predict reverseString output; trace repeat for small n.
Trace reverseString("cat").
Watch the unwind build the reversed string.
Calls down:
- reverseString("cat") = reverseString("at") + 'c'
- reverseString("at") = reverseString("t") + 'a'
- reverseString("t") returns "t" (base case, length ≤ 1)
Returns up:
- reverseString("at") = "t" + 'a' = "ta"
- reverseString("cat") = "ta" + 'c' = "tac"
Answer: "tac".
Why this matters. The first char of the input ends up last in the output because it's appended last (after the recursion finishes). Swapping the order — charAt(0) + reverseString(...) — would return "cat" unchanged.
String/sum recursion pitfalls.
Most are off-by-one or wrong concat order.
- Wrong concatenation order. Reverses what you want — or doesn't.
- Forgetting substring(1). Recursing on the full string never terminates.
- Base case at length 0 only. Check length ≤ 1 for safety.
- Base case returns null. Should return empty string.
- Sum base returns 1. Should be 0; otherwise adds an extra.
String + sum recursion templates.
Identity and combination.
Recursive Building Reference
AP Quick Reference| Type | Identity | Combine |
|---|---|---|
| String build | "" | + (concat) |
| Sum | 0 | + (add) |
| Peel from String | charAt(0), substring(1) | — |
| Peel from int | n % 10, n / 10 | — |
How to master string and sum recursion.
Identity at base; combine at each level.
Use the three practice tools below this lesson in order:
- MCQ Practice — for each option, identify the base case identity and the combination operator.
- Java Lab — write reverseString, repeat, sumDigits. Trace on small inputs to verify order.
- FRQ Practice — decide concat order based on whether you want to build forward or reverse.
If you can identify the identity (empty or zero) and the combine operator (concat or add), the rest follows.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 11 Topic 03 Recursive String Building and Summation FRQ Practice
AP-style topic practice assessment