Doing math on characters — the Caesar cipher idea.
Java chars convert to ints automatically. That lets you shift letters by adding numbers — and modulo keeps the result in the alphabet.
Java chars are stored as integer codes (ASCII / Unicode). 'A' is 65; 'B' is 66; 'a' is 97. This means you can do arithmetic on them: (char)(c + 1) produces the next character.
The Caesar cipher shifts every letter by a fixed amount, wrapping around the alphabet. The wrap is handled with % on the letter index (0-25).
On the AP exam, char arithmetic appears in shift-cipher FRQs and char-to-index conversion problems.
Subtract base; shift; modulo 26; add base back.
The four-step Caesar pattern.
Basic arithmetic
char c = 'A';
char next = (char)(c + 1); // 'B'
int diff = 'D' - 'A'; // 3
Adding a number to a char produces an int; cast back to char to get a character.
Caesar shift with wrap
public char shift(char c, int amount) {
if (c >= 'a' && c <= 'z') {
int index = c - 'a'; // 0-25
int shifted = (index + amount) % 26; // wrap
return (char)('a' + shifted); // back to char
}
return c; // non-letters unchanged
}
Why the four steps?
- Subtract 'a' to map letter to 0-25.
- Add amount to shift.
- Modulo 26 to wrap past 'z' back to 'a'.
- Add 'a' to convert back to a letter.
Shifting a whole String
public String encode(String s, int amount) {
String result = "";
for (int i = 0; i < s.length(); i++) {
result += shift(s.charAt(i), amount);
}
return result;
}
Char arithmetic vocabulary.
Used in cipher problems.
- ASCII / Unicode — numeric encoding of characters.
- Char to int — automatic conversion in arithmetic.
- Cast back —
(char)turns an int into a char. - Base letter — 'a' or 'A' as the index-0 starting point.
- Caesar cipher — fixed-shift letter substitution.
- Modular wrap — using
% 26to cycle within the alphabet.
How char arithmetic is tested.
The four-step shift and cast direction dominate.
- Subtract base before modulo. Otherwise you wrap by ASCII code, not letter position.
- Cast back to char. Without it, the result is an int.
- Handle case separately. 'a' and 'A' need their own bases.
- Non-letters pass through. Spaces, punctuation usually unchanged.
Shift "xyz" by 3.
Two letters wrap.
Trace 'x', 'y', 'z' shifted by 3:
- 'x': index 23; 23+3=26; 26 % 26 = 0; 'a' + 0 = 'a'.
- 'y': index 24; 24+3=27; 27 % 26 = 1; 'a' + 1 = 'b'.
- 'z': index 25; 25+3=28; 28 % 26 = 2; 'a' + 2 = 'c'.
Result: "abc".
Without the modulo: 'x' + 3 = '{' (code 123) — past the alphabet. The wrap is essential.
Char arithmetic pitfalls.
Wrong base or missing cast.
- No subtract before modulo. Wraps using ASCII codes, not letter positions.
- No cast back. Method returns the int, not the char.
- Wrong base for case. Using 'a' for uppercase yields garbage.
- Modulo on negative. Negative shifts need
((x % 26) + 26) % 26. - Treating non-letters. Don't shift spaces or digits — return as-is.
Caesar shift template.
Four steps in one line.
Char Arithmetic Reference
AP Quick Reference| Step | Code | Effect |
|---|---|---|
| To index | c - 'a' | 0-25. |
| Shift | + amount | Move. |
| Wrap | % 26 | Cycle. |
| To char | (char)('a' + shifted) | Cast. |
How to master char arithmetic.
Memorize the four-step pattern.
- MCQ Practice — for each shift, verify all four steps appear in correct order.
- Java Lab — implement encode and decode for the Caesar cipher. Test wraparound at 'z'.
- FRQ Practice — separate cases by uppercase, lowercase, and other; cast back to char before storing.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 12 Topic 06 Char Arithmetic and Modular Shift FRQ Practice
AP-style topic practice assessment