01
Overview

Scanning a string character by character.

Loop with charAt to inspect each character; count matches or detect presence.

Character scanning visits every character in a String using an indexed loop. At each position, charAt(i) returns the character, which can then be tested or counted.

Common scanning tasks: count vowels, count letters, find positions, detect presence. The pattern is identical to array traversal but uses length() and charAt(i) instead of length and arr[i].

On the AP exam, character scanning is in nearly every String FRQ.

02
Core Concept

Loop with index; charAt(i) at each step.

Standard String traversal idiom.

Count vowels

public int countVowels(String s) {
    int count = 0;
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
            count++;
        }
    }
    return count;
}

Count target character

public int countChar(String s, char target) {
    int count = 0;
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == target) count++;
    }
    return count;
}

Find first position (manual indexOf)

public int findChar(String s, char target) {
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == target) return i;
    }
    return -1;
}

Equivalent to s.indexOf(target). Both scan left-to-right and return the first match.

Why == on chars

Chars are primitives. == compares their values directly. .equals() is for object types (including String — but not char).

03
Key Vocabulary

Scanning vocabulary.

Used in text-processing problems.

Vocabulary
  • Character scanning — visiting each char of a String.
  • charAt(i) — access char at index i.
  • length() — total chars in String.
  • indexOf — built-in first-match search.
  • Linear search — sequential scan until found.
  • Sentinel return — -1 when not found.
04
AP Exam Focus

How character scanning is tested.

Loop bound and char comparison dominate.

Exam Focus
  • Loop to length(). Exclusive — last valid index is length - 1.
  • charAt for access. Bracket notation doesn't work.
  • == for chars. equals() for Strings.
  • Use || for "any of these chars". Vowel checks chain conditions.
05
Worked Example

Trace countVowels("Education").

Lowercase only for this version.

Worked Example AP-style reasoning

Input: "Education" (note capital E).

  • 'E' at 0: not in lowercase list → skip.
  • 'd' at 1: skip.
  • 'u' at 2: vowel → count = 1.
  • 'c' at 3: skip.
  • 'a' at 4: vowel → count = 2.
  • 't' at 5: skip.
  • 'i' at 6: vowel → count = 3.
  • 'o' at 7: vowel → count = 4.
  • 'n' at 8: skip.

Answer: 4. To also catch 'E' (uppercase), extend the condition or use Character.toLowerCase(c) first.

06
Common Mistakes

Scanning pitfalls.

Most are char-vs-String confusion.

Watch Out
  • Using .equals() on chars. Compile error.
  • Loop to length() - 1. Misses the last char.
  • Bracket access. s[i] doesn't compile.
  • Single quotes vs double. 'a' is char; "a" is String.
  • Case mismatch. 'a' and 'A' are different.
07
Reference Table

Scanning template.

Standard loop structure.

Character Scanning Reference

AP Quick Reference
ElementFormNote
Loopfor (int i = 0; i < s.length(); i++)Standard.
Accesss.charAt(i)Returns char.
Comparec == targetPrimitive equality.
CountIncrement in match branchInitialize at 0.
08
Practice Tip

How to master character scanning.

Loop, charAt, compare with ==.

Practice Strategy
  • MCQ Practice — for each option, verify the loop bound is < length() and char comparison uses ==.
  • Java Lab — write countVowels, countDigits, findFirst. Test on mixed-case strings.
  • FRQ Practice — the standard scan loop is muscle memory; rebuild it from scratch each time.
09
Section · 09

Practice — attempt these now.

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

FRQ Practice 30 min 16 marks Pending

AP CSA Unit 12 Topic 05 Character Scanning and indexOf FRQ Practice

AP-style topic practice assessment

Start