Help with Arduino Strings

Hi, I have the following code, rotate Left works, Rotate right doesnt can anyone help? I’m from a Basic/Assembler background so not sure what isnt letting the second function work?

void rotateStringLeft(char string[]) {
  char temp = string[0] ;
  for (int j=0 ; j < strlen(string) ;j++) {
  string[j] = string[j+1] ;  
  string[strlen(string)] =temp ;

void rotateStringRight(char string[]) {
  char temp = string[strlen(string)] ;
  for( int j = 0 ; j < strlen(string) ; j++) {
    string[j+1] = string[j] ;
  string[0] =temp ;

You need to begin at the end and work your way backwards or else on char is moved again and again across the entire string.

First of all, the last character in a string is string[strlen(string)-1].
That is: If strlen() is 10, the chars are string[0], string[1], … , string[9]. String [10] will be the zero-byte that terminates the string. You do not (that’s not) want to shift the zero-byte, just the other 9.


To rotate right, you can do something like the following:

Set temp to the right-most character in the string.

Loop as follows:
   Start with the right-most character and go down to the second character:
       set string[j] = string[j-1]

After the loop, set string[0] to temp.