given a string strwhose length lengthand number of revolutionsn
rotation to the left is equivalent
reverse(str, 0, n);
reverse(str, n, length);
reverse(str, 0, length);
turning right is equivalent
reverse(str, 0, length - n);
reverse(str, length - n, length);
reverse(str, 0, length);
Now you just need the inverse function.
Update: I was thinking about how you can use mod so that you always rotate in the right direction depending on the sign n.
eg.
int mod = n % length;
if (mod != 0) {
reverse(str, 0, mod);
reverse(str, mod, length);
reverse(str, 0, length);
}
going through various occasions
if n == 5 and length = 10, mod = 5
n == 16 length = 10, mod = 6 - 16 = 6
n == 0 length = anything, mod = 0
n == -1 length = 10, mod = 9 - 1 , 9
n == -15 length = 9, mod = 3 - 15 , 3