01
Overview

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.

02
Core Concept

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 ("" or 0).
  • Recursive case: isolate one piece (first char, last digit) and combine with the recursive call.
  • Direction: input shrinks by one piece each call.
03
Key Vocabulary

String and summation vocabulary.

Used in building and accumulating problems.

Vocabulary
  • 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.
04
AP Exam Focus

How recursive building is tested.

Substring boundaries and concatenation order dominate.

Exam Focus
  • Base case empty. "" for strings, 0 for 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.

05
Worked Example

Trace reverseString("cat").

Watch the unwind build the reversed string.

Worked Example AP-style reasoning

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.

06
Common Mistakes

String/sum recursion pitfalls.

Most are off-by-one or wrong concat order.

Watch Out
  • 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.
07
Reference Table

String + sum recursion templates.

Identity and combination.

Recursive Building Reference

AP Quick Reference
TypeIdentityCombine
String build""+ (concat)
Sum0+ (add)
Peel from StringcharAt(0), substring(1)
Peel from intn % 10, n / 10
08
Practice Tip

How to master string and sum recursion.

Identity at base; combine at each level.

Practice Strategy

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.

09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 18 marks Pending

AP CSA Unit 11 Topic 03 Recursive String Building and Summation FRQ Practice

AP-style topic practice assessment

Start