Rotate a string in c++?

前端 未结 9 584
梦毁少年i
梦毁少年i 2020-12-29 00:57

I\'m looking for a way to rotate a string in c++. I spend all of my time in python, so my c++ is very rusty.

Here is what I want it to do: if I have a strin

9条回答
  •  自闭症患者
    2020-12-29 01:30

    Here is code in C that does not use any external functions: It rotates the string in-place both forward and backwards by any value no matter how big.

    int stringRotate(int value)
      {
      unsigned long   I,J,K;
      unsigned long   index0;
      unsigned long   temp1,temp2;
      unsigned long   length;
    
      length = stringLength;
    
      if (value < 0)
        value = length - ((0 - value) % length);
    
      if (value > length)
        value = value % length;
    
      J = 0;
      index0 = J;
      temp1 = stringData[J];
    
      for (I = 0;I < length;I++)
        {
        K = (J + value) % length;
        temp2 = stringData[K];
        stringData[K] = temp1;
    
        J = K;
    
        temp1 = temp2;
    
        if (J == index0)
          {
          J++;
          index0 = J;
          temp1 = stringData[J];
          }
        }
    
      return 1;
      }
    

    To rotate the string both forward and backwards would be a bit tedious so it is better to only do forward rotate and calculate the correct value for backwards.Also if the rotation value is more than the length of the string, then we can just clip it since the result would be the same anyway.

    value = length - ((0 - value) % length) : means if the rotation value is negative, then set the value to the length of the string, minus the positive result of the remainder of dividing the value by the length of the string. For example: rotating a string of length 10 by -9 positions would be the same as rotating by +1. Rotating the same string by -19 positions would also be the same as rotating by plus one. value = value % length : means if the positive value is more than the string length, then divide by the string length and take the remainder. The result would be the same as if we just did it the long way.

    To do the rotation in place, we are going to need to jump by the value of of the rotation to swap characters that are that far apart. We start at position zero, move forward by the rotation value and continue jumping by that amount. if we go past the end of the string, we simply wrap back to the beginning. The problem is that if the value is an even number, we will end up right where we started and will miss all the odd characters. The variable index0 is there to indicate where we started from. If we end up back at this index then we need to move forward by one index position and continue jumping. We continue to do this until all the characters are swapped At this point we need two temporary variables to do the swapping in place. J is the starting position. We move the character at index J to the first temporary variable. Now we loop I to the length of the string. K is the destination index, J plus the rotation value Wrapped around the end if needed. Move the character at index K to the second temporary variable. Put the character from index J into index K using the first temporary variable. By the way the reason why we do not just move the character directly from index J into index K is because of the last part of the loop, the indices may change in between loops but the characters in temp1 should not. Now we exchange the temp1 with temp2. This last part is for where the value is an even number and we are back where we started. This will happen by times the rotation value minus one. increment index J by one and reset the starting values. Loop until done.

    A video demonstration can be found here: https://www.youtube.com/watch?v=TMzaO2WzR24

提交回复
热议问题