#K7721. Shift Cipher Operation
Shift Cipher Operation
Shift Cipher Operation
This problem requires you to implement a shift cipher. Given a string of alphanumeric characters, you need to shift each alphabetical character by a given integer value. Non-alphabetical characters (e.g., digits, punctuation) must remain unchanged. The cipher only operates on alphabetical characters and always converts them to lowercase. The shifting wraps around the alphabet. In mathematical terms, for a character c, the new character is computed as:
$$ new = (\text{ord}(c) - \text{ord}('a') + shift) \mod 26 + \text{ord}('a') $$
where ord(c)
is the ASCII value of c
, and the modulo operation ensures wrapping from 'z'
back to 'a'
.
Examples:
- Input: "Hello, World!" with shift 3 results in "khoor, zruog!"
- Input: "Python 3.8" with shift 5 results in "udymts 3.8"
- Input: "abcdefg" with shift 2 results in "cdefghi"
inputFormat
The input consists of two lines. The first line contains the text (a string of alphanumeric characters and punctuation). The second line contains an integer representing the shift value.
Example:
Hello, World! 3
outputFormat
Output the shifted text as a single line to stdout.
Example:
khoor, zruog!## sample
Hello, World!
3
khoor, zruog!