- 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
- Input:
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!
- Input:
Test Cases
Run your program with the following inputs:
ABC, shift = 1 →BCDXYZ, shift = 3 →ABChello, shift = 5 →mjqqtKhoor, shift = 3 (decrypt) →HelloAttack 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.