Unit 2 Day 17 String Algorithms

Unit 2: Selection and Iteration
Day 17 PracticeMedium
Focus: String traversal and manipulation

Today's Question

What is returned by mystery("hello")?
class="apcs-keyword">public static String mystery(String s) {
    String result = class="apcs-string">"";
    for (int i = s.length() - 1; i >= 0; i--) {
        result += s.charAt(i);
    }
    return result;
}

The loop traverses the string backward (from index 4 to 0), appending each character to result. This reverses the string: "olleh".

Key Concept

Traversing a string backward by starting at length()-1 and decrementing is a common pattern for reversing strings.

Want More Practice?

Master AP CSA with comprehensive resources

Unit 2 Study Guide Practice FRQs
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.