There's a simpler, more intuitive solution, which also more efficient. The trick is :

  • Take the first letter.
  • Compute where it should go after the rotate (addition with a modulo)
  • Save the target letter in a temp variable X
  • Replace the target letter by the first letter
  • Compute where should X go after the rotate
  • etc... n times, n being the length of the string.

Here a python snippet (sorry about the closing brackets, i don't know how to escape them):

def rotate_string(string, d):
old_char = string[0)
current_index = 0
for i in range(len(string)):
target_index = (current_index + d) % len(string)
temp_char = string [ target_index )
string [ target_index ) = old_char
current_index = target_index
old_char = temp_char
return string