beginner18 min

Coding and Decoding

Decode fixed-shift letter ciphers and apply the same rule to encode new words.

What you'll learn

  • Explain how a fixed alphabet-shift cipher encodes and decodes text
  • Encode a word given a shift value, including wrapping past Z back to A
  • Decode a word by reversing the same shift

Prerequisites

Explanation

Coding-decoding questions give you a small number of coded examples and ask you to find the rule, then apply it to a new word. The simplest and most common rule in placement tests is a fixed alphabet shift: every letter moves forward (or backward) by the same number of positions.

If CAT is coded as FDW, look at each letter: C (position 3) becomes F (position 6), A (1) becomes D (4), T (20) becomes W (23). Every letter moved forward by exactly 3 positions -- that's the rule. Once you know the shift, you can encode any new word the same way, or decode a coded word by shifting backward by the same amount.

The one detail that trips people up is wrapping: what happens when a shift pushes a letter past Z? The alphabet wraps back to A. Shifting X by 3 doesn't go past Z into nothing -- it wraps: X (24) -> Y (25) -> Z (26) -> A (1). So XYZ shifted by 3 becomes ABC, not an error or an out-of-range letter. The same wrapping applies going backward: decoding A with a shift of 3 wraps back to X.

A second thing to watch for: don't assume every coding scheme is a simple shift. Some use position-based substitution, reversal, or a different rule per letter position. The safe habit is to verify your hypothesis against a second coded example before applying it to a brand-new word -- one matching example could be a coincidence, but two confirms the rule.

Example

Shifting a single letter forward by a fixed amount, wrapping past Z.

function shiftChar(ch, shift) {
  const code = (((ch.charCodeAt(0) - 65 + shift) % 26) + 26) % 26;
  return String.fromCharCode(65 + code);
}
// shiftChar('A', 3) -> 'D'
// shiftChar('X', 3) -> 'A' (wraps)

Guided exercise

Guided exercise

Using the provided shiftChar helper, write encodeShift(word, shift) that shifts every uppercase letter in word forward by shift positions, wrapping past Z, and returns the resulting word.

Checks: Encodes a short word with shift 3 · Encodes a longer word with shift 1 · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Independent exercise

Independent exercise

Using the provided shiftChar helper, write decodeShift(word, shift) that reverses a shift-cipher encoding by shifting every letter backward by shift positions.

Checks: Decodes a short word with shift 3 · Decodes a longer word with shift 1 · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Common mistakes

  • Assuming a shift cipher never wraps, and getting confused when a letter near Z or A is involved.
  • Applying the shift in the wrong direction when decoding instead of encoding.
  • Committing to 'it's a shift cipher' from one example without checking a second coded word.

Knowledge check

Knowledge check

1. If CAT is coded as FDW using a fixed forward alphabet shift, what shift value was used?
2. Using a shift of 5, what does the letter V encode to (wrapping past Z if needed)?
3. Why is it risky to assume a coding scheme always uses a simple fixed shift?

Takeaway

A coding scheme's rule is only confirmed once it correctly predicts a second example, not just the first one you were given.

Summary

Fixed-shift ciphers move every letter forward or backward by a constant amount, wrapping past Z or A as needed. Decoding reverses the same shift applied during encoding.

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.