#C3559. Shift Alphabet Characters
Shift Alphabet Characters
Shift Alphabet Characters
This problem requires you to implement a function shift_string
that shifts each letter in a given string by a specified number of positions in the alphabet. The shifting is cyclic: for example, shifting z by 1 results in a. The case of each letter is preserved, and non-alphabet characters remain unchanged.
Formally, for an alphabet character c and an integer shift n, the new character is given by:
\( \text{newChar} = \begin{cases} \text{chr}((\text{ord}(c)-\text{ord}('a')+n) \bmod 26 + \text{ord}('a')), & \text{if } c \text{ is lowercase}\\[6pt] \text{chr}((\text{ord}(c)-\text{ord}('A')+n) \bmod 26 + \text{ord}('A')), & \text{if } c \text{ is uppercase} \end{cases} \)
For instance, shifting "abc" by 2 produces "cde", and shifting "Hello, World!" by 5 produces "Mjqqt, Btwqi!".
inputFormat
The input consists of two lines:
- The first line is a string
s
. It may contain spaces, punctuation, and digits. - The second line is an integer
n
representing the number of positions to shift each alphabet character.
outputFormat
Output the resulting string after shifting its alphabet characters by n
positions.
abc
2
cde