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.
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.
Char vocabulary.
Used in text-processing problems.
- 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.
How char operations are tested.
charAt and comparison are core.
- 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.
Count uppercase letters.
Range comparison on chars.
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.
Char pitfalls.
Most confuse char with String.
- 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; usecharAt(i). - Case mismatch. 'a' and 'A' are different chars.
Char operations.
Access, compare, count.
Char Reference
AP Quick Reference| Operation | Syntax | Note |
|---|---|---|
| Access | s.charAt(i) | Returns char. |
| Compare | c == target | Primitive comparison. |
| Range | c >= 'A' && c <= 'Z' | Uppercase test. |
| To index | c - 'a' | Maps to 0-25. |
How to master char processing.
Drill charAt and direct comparison.
- 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.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 09 Topic 04 Char Counting and Comparison FRQ Practice
AP-style topic practice assessment