mediumUnit 3 · Strings
P9. Caesar Cipher
Strings
In a Caesar cipher each letter is shifted forward in the alphabet by k places, wrapping around from z to a. Uppercase letters stay uppercase, lowercase letters stay lowercase, and all other characters are unchanged.
Encrypt the text with shift k, then decrypt the encrypted text (shift back by k) to show the original is recovered. Use ord() and chr().
Input Format
- Line 1: the text
- Line 2:
k; the shift
Output Format
- Line 1:
Encrypted: <text> - Line 2:
Decrypted: <text>
Constraints
- 1 ≤ length of the text ≤ 200
- 0 ≤
k≤ 25
Sample Test Cases
Sample 1
Input
Hello, World! 3
Output
Encrypted: Khoor, Zruog! Decrypted: Hello, World!
Sample 2
Input
xyz ABC 2
Output
Encrypted: zab CDE Decrypted: xyz ABC
Submitting checks 2 sample cases and 3 hidden test cases.