#C12268. Alphabetical Shift of a String
Alphabetical Shift of a String
Alphabetical Shift of a String
You are given a string s
consisting of lowercase letters and an integer shift
. Your task is to shift each character in the string by shift
positions in the alphabet (wrapping around as necessary). This is essentially a variation of the Caesar cipher.
For example, when shifting "xyz" by 2 positions, the result becomes "zab". Similarly, shifting "bcd" by -1 results in "abc".
The shifting operation is defined as:
\( \text{shift_string}(s, shift) = \text{each character } c \text{ in } s \text{ is replaced by } \text{chr}(((\text{ord}(c)-\text{ord}('a')+shift) \mod 26) + \text{ord}('a')) \)
Implement the function to perform the alphabetical shift as described.
inputFormat
The input consists of exactly two lines:
- The first line contains a non-empty string
s
of lowercase English letters. - The second line contains an integer
shift
indicating how many positions each letter in the string should be shifted.
Note that shift
can be positive, zero, or negative.
outputFormat
Output a single line containing the resulting string after performing the shift operation on every character of s
.
xyz
2
zab