---
title: "String Algorithms | AP Computer Science A 2.10"
description: "Review AP CSA 2.10 string algorithms, including charAt, substring scanning, counting substrings, reversing strings, loop bounds, and String immutability."
canonical: "https://fiveable.me/ap-comp-sci-a/unit-2/developing-algorithms-using-strings/study-guide/hDOL1VhnMQFPkBf6xMMW"
type: "study-guide"
subject: "AP Computer Science A"
unit: "Unit 2 – Selection and Iteration"
lastUpdated: "2026-06-09"
---

# String Algorithms | AP Computer Science A 2.10

## Summary

Review AP CSA 2.10 string algorithms, including charAt, substring scanning, counting substrings, reversing strings, loop bounds, and String immutability.

## Guide

String algorithms combine loops with `String` methods like `charAt`, `substring`, `length`, and `indexOf` to examine text one character or one substring at a time. The three standard goals are checking whether substrings have a property, counting substrings that meet criteria, and building a new string with the characters reversed. For [AP Computer Science A](/ap-comp-sci-a "fv-autolink"), pay close attention to [loop](/ap-comp-sci-a/key-terms/loop "fv-autolink") bounds and substring indices.

## How Do String Algorithms Work in AP CSA?

String algorithms use loops to visit characters or substrings in a predictable order. Most AP CSA string tasks ask you to check a property, count matching substrings, or build a new string, so the key is choosing safe loop bounds and using `charAt`, `substring`, `length`, and `.equals()` correctly.

## Why This Matters for the AP Computer Science A Exam

String processing shows up across both sections of the AP Computer Science A exam. On multiple choice, you trace loops that call String methods and determine output or pick the correct [algorithm](/ap-comp-sci-a/key-terms/algorithm "fv-autolink"), so you need to read loop bounds and [index](/ap-comp-sci-a/key-terms/index "fv-autolink") math precisely. On free-response code writing, you may be asked to write a method that uses loops and conditionals along with String methods to analyze or transform text, which is exactly the pattern in this topic. Getting comfortable with character traversal and substring scanning makes these questions far more predictable.

## Key Takeaways

- [Traverse](/ap-comp-sci-a/key-terms/traverse "fv-autolink") a string with a loop from index `0` to `length() - 1`, using `charAt(i)` to read each character.
- Build new strings with concatenation because [String objects](/ap-comp-sci-a/unit-4/recursive-searching-and-sorting/study-guide/tP6n1uldmjMrgteAVspr "fv-autolink") cannot be changed in place.
- Use `substring(start, end)` knowing that `start` is inclusive and `end` is exclusive.
- Standard string tasks include checking a property of substrings, counting substrings that match criteria, and reversing characters.
- Use `.equals()` to compare String content, not `==`.
- Watch loop bounds and index math to avoid `StringIndexOutOfBoundsException` and off-by-one errors.

## Core String Patterns

### Character-by-Character Traversal

The most basic pattern reads each character using the string's length to control the loop and `charAt()` to access each position.

```java
String word = "hello";
for (int i = 0; i < word.length(); i++) {
    char ch = word.charAt(i);
    // Process this character
}
```

This pattern is the base for counting characters, finding specific letters, and checking string properties.

### Building a New String

Many string algorithms build a result gradually. Since strings are [immutable](/ap-comp-sci-a/key-terms/immutable "fv-autolink"), you create a new string by concatenating pieces as you go.

```java
String original = "programming";
String result = "";
for (int i = 0; i < original.length(); i++) {
    char ch = original.charAt(i);
    if (ch != 'g') {       // example condition
        result += ch;
    }
}
```

### Substring Scanning

Sometimes you compare pieces of a string instead of single characters. Slide a window across valid starting positions and compare each piece to a target.

The key is keeping your starting index in range so the substring call never runs past the end of the string.

### Pattern Recognition

When you read a string problem, identify which goal it fits:

- Counting: how many times does something occur?
- Finding: where is the first or last occurrence?
- Filtering: which characters or substrings meet a condition?
- Transforming: how do we build a modified version?
- Comparing: how do parts of the string relate to each other?

## Code Examples

### Counting Characters

```java
public class CharacterCounting {
    // Count occurrences of a specific character
    public static int countChar(String text, char target) {
        int count = 0;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == target) {
                count++;
            }
        }
        return count;
    }
    
    // Count vowels in a string
    public static int countVowels(String text) {
        int count = 0;
        String vowels = "aeiouAEIOU";
        
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            if (vowels.indexOf(ch) >= 0) {  // Found in vowels string
                count++;
            }
        }
        return count;
    }
    
    public static void main(String[] args) {
        System.out.println(countChar("programming", 'g'));  // 2
        System.out.println(countVowels("programming"));     // 3
    }
}
```

### Finding Patterns

```java
public class PatternFinding {
    // Find first occurrence of a character
    public static int findFirst(String text, char target) {
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == target) {
                return i;  // Return index where found
            }
        }
        return -1;  // Not found
    }
    
    // Find last occurrence of a character  
    public static int findLast(String text, char target) {
        for (int i = text.length() - 1; i >= 0; i--) {
            if (text.charAt(i) == target) {
                return i;
            }
        }
        return -1;
    }
    
    // Check if string contains only digits
    public static boolean isAllDigits(String text) {
        if (text.length() == 0) return false;
        
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            if (ch < '0' || ch > '9') {  // Not a digit
                return false;
            }
        }
        return true;
    }
    
    public static void main(String[] args) {
        System.out.println(findFirst("programming", 'r'));  // 1
        System.out.println(findLast("programming", 'r'));   // 4
        System.out.println(isAllDigits("12345"));           // true
        System.out.println(isAllDigits("123a5"));           // false
    }
}
```

### String Transformation

```java
public class StringTransformation {
    // Remove all occurrences of a character
    public static String removeChar(String text, char toRemove) {
        String result = "";
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            if (ch != toRemove) {
                result += ch;
            }
        }
        return result;
    }
    
    // Reverse a string
    public static String reverse(String text) {
        String result = "";
        for (int i = text.length() - 1; i >= 0; i--) {
            result += text.charAt(i);
        }
        return result;
    }
    
    // Replace all occurrences of one character with another
    public static String replaceChar(String text, char oldChar, char newChar) {
        String result = "";
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            if (ch == oldChar) {
                result += newChar;
            } else {
                result += ch;
            }
        }
        return result;
    }
    
    public static void main(String[] args) {
        System.out.println(removeChar("programming", 'g'));      // "prorammin"
        System.out.println(reverse("hello"));                    // "olleh"
        System.out.println(replaceChar("hello", 'l', 'x'));     // "hexxo"
    }
}
```

The `reverse` method here is the standard "create a new string with the characters reversed" algorithm, done by walking the string from the last index back to `0`.

### Substring Analysis

```java
public class SubstringAnalysis {
    // Count occurrences of a substring
    public static int countSubstring(String text, String target) {
        int count = 0;
        int targetLen = target.length();
        
        // Check each possible starting position
        for (int i = 0; i <= text.length() - targetLen; i++) {
            String sub = text.substring(i, i + targetLen);
            if (sub.equals(target)) {
                count++;
            }
        }
        return count;
    }
    
    // Find all positions where substring occurs
    public static void findAllOccurrences(String text, String target) {
        int targetLen = target.length();
        
        for (int i = 0; i <= text.length() - targetLen; i++) {
            String sub = text.substring(i, i + targetLen);
            if (sub.equals(target)) {
                System.out.println("Found '" + target + "' at index " + i);
            }
        }
    }
    
    // Check if string is a palindrome
    public static boolean isPalindrome(String text) {
        int left = 0;
        int right = text.length() - 1;
        
        while (left < right) {
            if (text.charAt(left) != text.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
    
    public static void main(String[] args) {
        System.out.println(countSubstring("ababab", "ab"));  // 3
        findAllOccurrences("programming", "gr");             // Found 'gr' at index 3
        System.out.println(isPalindrome("racecar"));         // true
        System.out.println(isPalindrome("hello"));           // false
    }
}
```

`countSubstring` is the standard "count the substrings that meet criteria" pattern, and `isPalindrome` checks a property of the string by comparing characters from both ends.

### Complex Pattern Processing

```java
public class ComplexPatterns {
    // Extract all digits from a string
    public static String extractDigits(String text) {
        String digits = "";
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            if (ch >= '0' && ch <= '9') {
                digits += ch;
            }
        }
        return digits;
    }
    
    // Count words in a string (simplified - splits on spaces)
    public static int countWords(String text) {
        if (text.length() == 0) return 0;
        
        int wordCount = 0;
        boolean inWord = false;
        
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            if (ch != ' ') {  // Non-space character
                if (!inWord) {
                    wordCount++;  // Starting a new word
                    inWord = true;
                }
            } else {  // Space character
                inWord = false;
            }
        }
        return wordCount;
    }
    
    // Remove consecutive duplicate characters
    public static String removeDuplicates(String text) {
        if (text.length() <= 1) return text;
        
        String result = "" + text.charAt(0);  // Start with first character
        
        for (int i = 1; i < text.length(); i++) {
            if (text.charAt(i) != text.charAt(i - 1)) {
                result += text.charAt(i);
            }
        }
        return result;
    }
    
    public static void main(String[] args) {
        System.out.println(extractDigits("abc123def456"));    // "123456"
        System.out.println(countWords("hello world test"));   // 3
        System.out.println(removeDuplicates("aabbccdd"));     // "abcd"
    }
}
```

## Common Errors and Debugging

### StringIndexOutOfBoundsException

This happens when you try to access a character beyond the string's length.

**Common cause**: Off-by-one errors in loop bounds or substring operations.

**Example scenario**:
```java
String word = "hello";
for (int i = 0; i <= word.length(); i++) {  // Bug: should be <, not <=
    System.out.println(word.charAt(i));  // Crashes when i equals length
}
```

**How to fix**: Always use `i < string.length()` for character access.

**Debugging tip**: When working with substrings, double-check that your end index doesn't exceed the string length.

### Incorrect Substring Bounds

Substring operations can be tricky because the end index is exclusive.

**Common cause**: Confusion about substring(start, end) [parameters](/ap-comp-sci-a/unit-4/recursion/study-guide/p4D3YegZCLwQ3KJVvsd4 "fv-autolink").

**Example scenario**:
```java
String text = "programming";
// Want to get "gram" (indices 3, 4, 5, 6)
String sub = text.substring(3, 7);  // Correct: end is exclusive, gives "gram"
String wrong = text.substring(3, 6); // Bug: would get "gra"
```

**How to fix**: Remember that substring(start, end) goes from start (inclusive) to end (exclusive), so the length of the result is `end - start`.

### Forgetting String Immutability

Strings can't be modified, so operations like += create new string objects each time.

**Common cause**: Trying to modify a string directly or not understanding concatenation.

**Example scenario**:
```java
String word = "hello";
// word.charAt(0) = 'H';  // Does not work - charAt cannot be assigned

// Use concatenation instead:
word = "H" + word.substring(1);  // Creates new string "Hello"
```

**How to fix**: Build a new string with concatenation as you traverse.

## How to Use This on the AP Computer Science A Exam

### Code Tracing

On multiple choice, expect to trace loops that call String methods and report the output. Track the loop index, the current character from `charAt(i)`, and any [accumulator](/ap-comp-sci-a/key-terms/accumulator "fv-autolink") string after each pass. Watch [for loops](/ap-comp-sci-a/unit-2/for-loops/study-guide/DJuLxKz6SiSAX2cEVmCt "fv-autolink") that run from the end of the string back to `0`, since those usually signal a reversal.

### Free Response

Free-response code writing often asks for a method that loops through a string and uses String methods to analyze or transform it. Read the specification closely: underline the [return type](/ap-comp-sci-a/key-terms/return-type "fv-autolink"), the parameters, and the exact behavior described in the examples. Then pick the matching pattern, whether that is counting, checking a property, or building a reversed string.

### Common Trap

Substring bounds and loop bounds cause most string mistakes. When scanning for a substring of length `targetLen`, your loop should run while `i <= text.length() - targetLen` so the final `substring(i, i + targetLen)` stays in range.

## Practice Problems

### Problem 1: Pig Latin Converter

Write a method that converts a single word to Pig Latin. If the word starts with a vowel, add "way" to the end. If it starts with a consonant, move the first letter to the end and add "ay".

```java
public static String toPigLatin(String word) {
    // Your solution here
}
```

**Solution**:
```java
public static String toPigLatin(String word) {
    if (word.length() == 0) return word;
    
    String vowels = "aeiouAEIOU";
    char firstChar = word.charAt(0);
    
    if (vowels.indexOf(firstChar) >= 0) {
        // Starts with vowel
        return word + "way";
    } else {
        // Starts with consonant
        return word.substring(1) + firstChar + "ay";
    }
}
```

**Test cases**: "apple" -> "appleway", "hello" -> "ellohay"

### Problem 2: Caesar Cipher

Write a method that shifts each letter in a string by a fixed number of positions in the alphabet:

```java
public static String caesarCipher(String text, int shift) {
    // Shift each letter by the specified amount
    // Your solution here
}
```

**Solution**:
```java
public static String caesarCipher(String text, int shift) {
    String result = "";
    
    for (int i = 0; i < text.length(); i++) {
        char ch = text.charAt(i);
        
        if (ch >= 'a' && ch <= 'z') {
            // Lowercase letter
            int shifted = ((ch - 'a' + shift) % 26 + 26) % 26;
            result += (char)('a' + shifted);
        } else if (ch >= 'A' && ch <= 'Z') {
            // Uppercase letter  
            int shifted = ((ch - 'A' + shift) % 26 + 26) % 26;
            result += (char)('A' + shifted);
        } else {
            // Not a letter, keep as-is
            result += ch;
        }
    }
    return result;
}
```

This uses [modular arithmetic](/ap-comp-sci-a/key-terms/modular-arithmetic "fv-autolink") to wrap around the alphabet so a [shift](/ap-comp-sci-a/unit-4/developing-algorithms-using-arraylists/study-guide/MKbteieYvLOpWIwfqiND "fv-autolink") past `z` returns to `a`.

### Problem 3: Count Matching Substrings

Write a method that counts how many times a target substring appears in a longer string, including [overlapping occurrences](/ap-comp-sci-a/key-terms/overlapping-occurrences "fv-autolink").

```java
public static int countMatches(String text, String target) {
    // Count overlapping occurrences of target in text
    // Your solution here
}
```

**Solution**:
```java
public static int countMatches(String text, String target) {
    int count = 0;
    int targetLen = target.length();
    
    if (targetLen == 0) return 0;
    
    for (int i = 0; i <= text.length() - targetLen; i++) {
        if (text.substring(i, i + targetLen).equals(target)) {
            count++;
        }
    }
    return count;
}
```

**Example**: `countMatches("ababab", "ab")` returns `3`, and `countMatches("aaaa", "aa")` returns `3` because overlaps are counted.

## Common Misconceptions

- Using `==` to compare String content. Use `.equals()`; `==` checks whether two [variables](/ap-comp-sci-a/unit-1/expressions-and-assignment-statements/study-guide/01dr6uUPDAn3SjtK2Psr "fv-autolink") point to the same [object](/ap-comp-sci-a/key-terms/object "fv-autolink"), not whether the characters match.
- Thinking `charAt()` lets you change a character. It only reads a character. To change text, build a new string with concatenation or `substring`.
- Assuming the `end` index of `substring(start, end)` is included. It is exclusive, so the result length is `end - start`.
- [Looping](/ap-comp-sci-a/unit-2/algorithms-with-selection-and-repetition/study-guide/42crNSZyW8IRsntk9IHe "fv-autolink") with `i <= text.length()` for character access. That goes one index too far and throws `StringIndexOutOfBoundsException`; use `i < text.length()`.
- For substring scanning, stopping the loop at `text.length()`. Stop at `text.length() - targetLen` so the substring call stays in range.
- Believing concatenation changes the original string. Each `+=` creates a new string; the original is unchanged because strings are immutable.

## Related AP Computer Science A Guides

- [2.1 Algorithms with Selection and Repetition](/ap-comp-sci-a/unit-2/algorithms-with-selection-and-repetition/study-guide/42crNSZyW8IRsntk9IHe)
- [2.7 While Loops](/ap-comp-sci-a/unit-2/while-loops/study-guide/7qGsGOh1UKALAWpJhZOi)
- [2.8 For Loops](/ap-comp-sci-a/unit-2/for-loops/study-guide/DJuLxKz6SiSAX2cEVmCt)
- [2.9 Implementing Selection and Iteration Algorithms](/ap-comp-sci-a/unit-2/implementing-selection-and-iteration-algorithms/study-guide/ulqF0nPukr6rbwgDTCuU)
- [2.6 Equivalent Boolean Expressions](/ap-comp-sci-a/unit-2/equivalent-boolean-expressions/study-guide/aMDnyFuOcAXnZigLW1vL)
- [2.11 Nested Iteration](/ap-comp-sci-a/unit-2/nested-iteration/study-guide/Buapg1KURHNbw6yRY8EZ)
- [2.12 Informal Code Analysis](/ap-comp-sci-a/unit-2/informal-code-analysis/study-guide/CR84MbOE4FDDoSVokDVZ)

## Vocabulary

- **character reversal**: The process of rearranging the characters in a string in reverse order, from last to first.
- **string algorithm**: Procedures or methods designed to perform operations on strings, such as searching, modifying, or analyzing text data.
- **substring**: Contiguous sequences of characters within a larger string.

## FAQs

### What are string algorithms in AP CSA?

String algorithms are step-by-step procedures that inspect or build String values. Common AP CSA tasks include finding substrings with a property, counting substrings that meet a condition, and creating a reversed string.

### How do you traverse a String in Java?

You usually traverse a String with a loop and an index. The index starts at 0 and should stay less than the string length, so charAt(i) can safely access each character.

### How do you scan substrings safely?

Use substring start and end indexes that stay inside the string. If the substring length is n, the last valid start index is usually str.length() - n because the end index is exclusive.

### Why should you use .equals() for Strings?

Use .equals() when you want to compare the text stored in two String objects. The == operator checks whether two references point to the same object, which is not the same as checking whether the text matches.

### Why are Java Strings immutable?

Strings are immutable because a String object cannot be changed after it is created. Methods like substring or concatenation return a new String value instead of editing the original string.

### How does Topic 2.10 show up on the AP CSA exam?

Topic 2.10 appears in code tracing and writing tasks where you process String values. Expect index bounds, charAt, substring, equals, concatenation, and loops that build or count string patterns.

## Structured Data

```json
{"@context":"https://schema.org","@type":"FAQPage","inLanguage":"en","mainEntity":[{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-2/developing-algorithms-using-strings/study-guide/hDOL1VhnMQFPkBf6xMMW#what-are-string-algorithms-in-ap-csa","name":"What are string algorithms in AP CSA?","acceptedAnswer":{"@type":"Answer","text":"String algorithms are step-by-step procedures that inspect or build String values. Common AP CSA tasks include finding substrings with a property, counting substrings that meet a condition, and creating a reversed string."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-2/developing-algorithms-using-strings/study-guide/hDOL1VhnMQFPkBf6xMMW#how-do-you-traverse-a-string-in-java","name":"How do you traverse a String in Java?","acceptedAnswer":{"@type":"Answer","text":"You usually traverse a String with a loop and an index. The index starts at 0 and should stay less than the string length, so charAt(i) can safely access each character."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-2/developing-algorithms-using-strings/study-guide/hDOL1VhnMQFPkBf6xMMW#how-do-you-scan-substrings-safely","name":"How do you scan substrings safely?","acceptedAnswer":{"@type":"Answer","text":"Use substring start and end indexes that stay inside the string. If the substring length is n, the last valid start index is usually str.length() - n because the end index is exclusive."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-2/developing-algorithms-using-strings/study-guide/hDOL1VhnMQFPkBf6xMMW#why-should-you-use-equals-for-strings","name":"Why should you use .equals() for Strings?","acceptedAnswer":{"@type":"Answer","text":"Use .equals() when you want to compare the text stored in two String objects. The == operator checks whether two references point to the same object, which is not the same as checking whether the text matches."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-2/developing-algorithms-using-strings/study-guide/hDOL1VhnMQFPkBf6xMMW#why-are-java-strings-immutable","name":"Why are Java Strings immutable?","acceptedAnswer":{"@type":"Answer","text":"Strings are immutable because a String object cannot be changed after it is created. Methods like substring or concatenation return a new String value instead of editing the original string."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-2/developing-algorithms-using-strings/study-guide/hDOL1VhnMQFPkBf6xMMW#how-does-topic-210-show-up-on-the-ap-csa-exam","name":"How does Topic 2.10 show up on the AP CSA exam?","acceptedAnswer":{"@type":"Answer","text":"Topic 2.10 appears in code tracing and writing tasks where you process String values. Expect index bounds, charAt, substring, equals, concatenation, and loops that build or count string patterns."}}]}
```
