#K34572. Rotate String
Rotate String
Rotate String
Given a string s
and an integer n
, write a program that rotates the string by n
positions. A positive value of \(n\) indicates a rotation to the right, whereas a negative value of \(n\) implies a left rotation by \(|n|\) positions. The rotation is defined in a cyclic manner. Formally, if \(L = \text{len}(s)\) then the effective rotation is \(r = n \mod L\) (if \(s\) is non-empty) and the rotated string is given by:
[ \text{rotated}(s) = s[-r:] + s[:-r] ]
For example, if s = "hello"
and n = 2
, the output is "lohel"
. If n
is negative, such as -1
with s = "abcd"
, then the output will be "bcda"
.
inputFormat
The input consists of two lines:
- The first line contains a non-empty string
s
(or possibly empty). - The second line contains an integer
n
, representing the number of positions to rotate the string.
outputFormat
Output a single line containing the rotated string. If s
is empty, output an empty string.
hello
2
lohel