01
Overview

Working with individual characters.

Iterating through a String, counting matching characters, comparing letters — the building blocks of text processing.

A char in Java is a single character. String.charAt(i) returns the character at index i. Characters compare with == (since they're primitives) and have ordinal value — 'A' is 65, 'B' is 66, and so on.

Counting characters that meet a condition combines String traversal with the charAt accessor. Comparison underlies any test like "is this letter a vowel?" or "is this digit?".

On the AP exam, char counting and comparison appear in String-manipulation FRQs and in tracing problems involving charAt.

02
Core Concept

Loop, charAt, test, count.

Standard String traversal with a per-char condition.

Char access

String s = "Hello";
char c = s.charAt(0);   // 'H'

Counting matching chars

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

Char comparison with ==

Unlike Strings, individual chars are primitives. Compare them with ==.

Counting vowels (compound condition)

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;
}

Char arithmetic

char c = 'D';
int offset = c - 'A';   // 3

Useful for mapping letters to indices 0-25.

03
Key Vocabulary

Char vocabulary.

Used in text-processing problems.

Vocabulary
  • char — primitive type for a single character.
  • charAt(i) — String method returning the char at index i.
  • Ordinal value — numeric code of a character.
  • Char literal — single character in single quotes ('a').
  • Char arithmetic — chars convert to int automatically.
04
AP Exam Focus

How char operations are tested.

charAt and comparison are core.

Exam Focus
  • charAt for access. Not s[i] — Java doesn't support bracket access on Strings.
  • == on chars. They're primitives.
  • Index bound. i < s.length(), not <=.
  • Char literals. Single quotes ('a'), not double.
05
Worked Example

Count uppercase letters.

Range comparison on chars.

Worked Example AP-style reasoning

Problem. Count uppercase letters in a String.

public int countUpper(String s) {
    int count = 0;
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (c >= 'A' && c <= 'Z') count++;
    }
    return count;
}

Trace. For "HelloWorld!": H, W are uppercase. count = 2.

06
Common Mistakes

Char pitfalls.

Most confuse char with String.

Watch Out
  • Using .equals() on chars. Chars are primitives; use ==.
  • Double quotes for char literal. "a" is String; 'a' is char.
  • Bracket indexing. s[i] doesn't compile; use charAt(i).
  • Case mismatch. 'a' and 'A' are different chars.
07
Reference Table

Char operations.

Access, compare, count.

Char Reference

AP Quick Reference
OperationSyntaxNote
Accesss.charAt(i)Returns char.
Comparec == targetPrimitive comparison.
Rangec >= 'A' && c <= 'Z'Uppercase test.
To indexc - 'a'Maps to 0-25.
08
Practice Tip

How to master char processing.

Drill charAt and direct comparison.

Practice Strategy
  • MCQ Practice — verify char vs String distinction and == vs .equals().
  • Java Lab — write countLetter, countVowel, countUpper. Test on mixed-case strings.
  • FRQ Practice — for "count characters" problems, use charAt with == on chars.
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 09 Topic 04 Char Counting and Comparison FRQ Practice

AP-style topic practice assessment

Start