← All coding problems
Caesar Cipher
easyPlacement Coding+10 XPTime limit 2s
Encrypt a message with a Caesar cipher: move every letter k places forward in the alphabet, wrapping around from z to a. Keep upper and lower case, and leave digits, spaces and punctuation unchanged. With k = 3, Hello, World! becomes Khoor, Zruog!.
Input:
- line 1:
k - line 2: the message (it does not start or end with a space)
Output: the encrypted message.
Constraints: 0 ≤ k ≤ 10⁹, 1 ≤ length ≤ 10⁵.
Think about it: first reduce k with k % 26. For a lowercase letter c, the new letter is 'a' + (c − 'a' + k) % 26; do the same with 'A' for capitals.
Sample input 1
3 Hello, World!
Sample output 1
Khoor, Zruog!
Sample input 2
1 xyz XYZ
Sample output 2
yza YZA
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).