#K47347. Shift Characters
Shift Characters
Shift Characters
You are given a string s
and an integer n
. Your task is to shift each alphabetical character in the string forward by n
positions in the alphabet. If the shift moves past 'z' (or 'Z' for uppercase), it wraps around to the beginning of the alphabet.
For example, given the string "hello, world!" and n = 3
, the output should be "khoor, zruog!".
Note: Non-alphabetical characters remain unchanged. Although the examples are provided in lowercase, your solution should handle both lowercase and uppercase letters appropriately.
The shifting can be mathematically expressed using the following formula for a character c (with a base of either \(a\) or \(A\)):
[ \text{shift}(c,n) = \big((c - \text{base} + n) \mod 26\big) + \text{base} ]
Implement a program that reads from stdin
and writes the result to stdout
.
inputFormat
The input consists of two lines:
- The first line contains the string
s
. - The second line contains an integer
n
representing the shift amount.
You may assume that s
has a reasonable length and n
is non-negative.
outputFormat
Output the transformed string after shifting each alphabetical character by n
positions. The result should be printed on a single line to stdout
.
hello, world!
3
khoor, zruog!