Bootcamp Curriculum • 5 Modules • 12 Chapters • Multi-Language Runner (Python, JS, Java, C++) • Interactive Memory Trace • AI Mentor
CHAPTER 10 · FUNCTIONS7 min read

Cipher logic & algorithmic encryption

Used by Julius Caesar to protect military orders, the Caesar Cipher shifts every letter in a message by a set number of positions across the alphabet. It is the perfect project to combine loops, lists, and functions.

By the end of this chapter, you can:
  • Use modulo arithmetic to wrap around an alphabet list.
  • Implement a function that shifts letters forwards for encryption.
  • Handle spaces and non-alphabet characters cleanly.
Explanation DepthTailor the level of detail to your learning goal
💻 Standard College Notes

Caesar cipher algorithm, modular arithmetic, and string parsing.

Shift each letter by shift positions: new_index = (index + shift) % 26. Modulo 26 ensures wrap-around when the shift exceeds the alphabet length.

💡Preserve case and non-alphabet characters (spaces, punctuation) unchanged.

The Caesar Cipher encryption algorithm

For every letter, find its index in the alphabet, add the shift amount, and use `% 26` to wrap back to the beginning if it passes 'z'.

example_1.py
Ctrl + ↵ to run
Terminal Output
Ready to execute
Click "▶ Run Code" or press Ctrl+Enter to compile & run this snippet.
✦ Interactive Dry-Run Visualizer

Memory Trace: Caesar Cipher Character Shift

Follow letter by letter index shift calculation in memory.

STEP1 / 4
1msg = "hi"👉 Executing
2alph = "abcdefghijklmnopqrstuvwxyz"
3# h is index 7 -> 7+3=10 (k)
4# i is index 8 -> 8+3=11 (l)
5print("kl")
VariableValue in MemoryData Type
msg"hi"str
Initialize message to encrypt

Message 'hi' loaded.

Console Stream (stdout)
(No output generated yet)
Line 1: msg = "hi"

🔧 IndexError Wrap-Around Bug

Encrypting 'z' with shift 3 caused an IndexError because 25 + 3 = 28 exceeded the 26-letter alphabet length without modulo.

⚡ +100 XP
fix_bug.pyPython 3.10
⚡ Quick Revision Quiz

Predict The Output (2 Questions)

Question 1 of 2:
What is (25 + 3) % 26?
Question 2 of 2:
How do you decrypt a Caesar cipher encrypted with shift = K?

Your lightbulb moment

✧

The key takeaway
The Caesar Cipher demonstrates how arithmetic, strings, loops, and functions unite to create classic encryption algorithms.

Make it yours

Try changing the shift to 5 and encrypt 'python' to see how the cipher text transforms.

Try editing the code in the playground above in Python, JavaScript, Java, or C++, and use the step-by-step Dry-Run Table to inspect how variables mutate in memory. If you get stuck, click the 🤖 AI Doubt Solver button on your screen for instant explanations!