01
Overview

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.

02
Core Concept

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;
}
03
Key Vocabulary

Char arithmetic vocabulary.

Used in cipher problems.

Vocabulary
  • 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 % 26 to cycle within the alphabet.
04
AP Exam Focus

How char arithmetic is tested.

The four-step shift and cast direction dominate.

Exam Focus
  • 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.
05
Worked Example

Shift "xyz" by 3.

Two letters wrap.

Worked Example AP-style reasoning

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.

06
Common Mistakes

Char arithmetic pitfalls.

Wrong base or missing cast.

Watch Out
  • 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.
07
Reference Table

Caesar shift template.

Four steps in one line.

Char Arithmetic Reference

AP Quick Reference
StepCodeEffect
To indexc - 'a'0-25.
Shift+ amountMove.
Wrap% 26Cycle.
To char(char)('a' + shifted)Cast.
08
Practice Tip

How to master char arithmetic.

Memorize the four-step pattern.

Practice Strategy
  • 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.
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 12 Topic 06 Char Arithmetic and Modular Shift FRQ Practice

AP-style topic practice assessment

Start