
Problem
Solution
Submissions
Caesar Cipher Encryption and Decryption
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 10
Write a Python program to implement Caesar Cipher encryption and decryption. Caesar Cipher is a substitution cipher where each letter in the plaintext is shifted a certain number of places down or up the alphabet. For example, with a shift of 3, A would be encoded as D, B would become E, and so on.
Example 1
- Input: text = "HELLO", shift = 3
- Output: "KHOOR"
- Explanation:
- Each letter is shifted 3 positions forward in the alphabet.
- H becomes K, E becomes H, L becomes O, L becomes O, O becomes R.
- Therefore, "HELLO" becomes "KHOOR".
- Each letter is shifted 3 positions forward in the alphabet.
Example 2
- Input: text = "KHOOR", shift = -3
- Output: "HELLO"
- Explanation:
- Each letter is shifted 3 positions backward in the alphabet.
- K becomes H, H becomes E, O becomes L, O becomes L, R becomes O.
- Therefore, "KHOOR" becomes "HELLO" (decryption).
- Each letter is shifted 3 positions backward in the alphabet.
Constraints
- 1 ≤ text.length ≤ 1000
- text consists of uppercase and lowercase English letters and spaces
- -25 ≤ shift ≤ 25
- Preserve the case of letters and keep spaces unchanged
- Time Complexity: O(n)
- Space Complexity: O(n)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Iterate through each character in the input text
- Check if the character is a letter (uppercase or lowercase)
- Calculate the shifted position using modular arithmetic
- Handle wrap-around cases (Z to A, z to a)
- Preserve spaces and special characters as they are
- Maintain the original case of the letters