Problem Set 12: Caesar Cipher

When you start a new problem set, your first instinct might be to open your computer and begin typing code right away. While this can feel productive, it often leads to frustration when things don't work as expected. Instead, take a few minutes to slow down and plan.

Here are some helpful strategies:

  1. Understand the problem clearly
    • Read the instructions carefully — twice if needed.
    • Ask yourself: What exactly is being asked?
  2. Break the problem into smaller steps
    • Think about the smallest possible actions the computer will need to perform.
    • For example: If the task is to find the first recurring letter in a word, what steps must happen first?
  3. Try solving it on paper first
    • Write out your steps in plain language (pseudocode).
    • Test your steps with a simple example before touching the keyboard.
  4. Translate your steps into code
    • Start small — write only a few lines at a time and test often.
    • Don't worry about perfection at first; get a working version, then improve it.
  5. Check your solution
    • Run it with different examples, including edge cases.
    • Ask: Does this solve the problem in all situations?

  • Read the question carefully (twice).
  • Break the task into the smallest steps.
  • Sketch or write pseudocode before coding.
  • Start small — test as you go.
  • Check your solution with different cases.

Problem Description

The Caesar cipher is one of the oldest and simplest encryption techniques.
It works by shifting letters of the alphabet by a fixed amount.

Example (shift = 3):

  • Plaintext: HELLO
  • Ciphertext: KHOOR

Because each letter has been shifted 3 places forward in the alphabet.


Learning Goals

By completing this problem, you should be able to:

  • Work with strings and characters.
  • Use loops to process text one character at a time.
  • Apply modulus arithmetic to wrap around the alphabet.
  • Understand the difference between encryption and decryption.

Tasks

Task 1: Encrypt with a Fixed Shift

  • Ask the user to enter a word.
  • Encrypt the word by shifting each letter 3 places forward in the alphabet.
  • Print the encrypted result.

Example:

  • Input: HELLO
  • Output: KHOOR

Task 2: General Shift

  • Modify your program so the user can enter any shift value.
  • If the shift goes past Z, wrap around to the beginning of the alphabet.

Example (shift = 5):

  • Input: WORLD
  • Output: BTWQI

Task 3: Decrypt Text

  • Write a program that takes ciphertext and the shift value.
  • Shifts letters backward to recover the original plaintext.

Example:

  • Input: KHOOR, shift = 3
  • Output: HELLO

Task 4: Handle Upper and Lower Case

  • Update your program so it works for both uppercase and lowercase letters.
  • Example:
    • Input: Hello, shift = 2
    • Output: Jgnnq

Task 5: Ignore Non-Letters

  • Update your program so punctuation, numbers, and spaces are not changed.
  • Example:
    • Input: Hello, World!, shift = 3
    • Output: Khoor, Zruog!

Test Cases

Run your program with the following inputs:

  1. ABC, shift = 1 → BCD
  2. XYZ, shift = 3 → ABC
  3. hello, shift = 5 → mjqqt
  4. Khoor, shift = 3 (decrypt) → Hello
  5. Attack at dawn!, shift = 4 → Exxego ex hear!

Extension Challenge (Optional)

  • Add a "brute force" decryption mode that tries all possible shifts (1–25) and prints all results.
    • Useful if you don’t know the shift.
  • Let the user choose encrypt or decrypt at the start.
  • Add support for multilingual alphabets (like including accented characters).

Success Criteria

  • You can explain how the Caesar cipher works.
  • Your program can encrypt and decrypt text correctly with any shift.
  • Your program passes the test cases.
  • (Extension) You can brute force decrypt without knowing the shift.