Rotate a string in C ++?

I am looking for a way to rotate a string in C ++. I spend all my time in python, so my C ++ is very rusty.

Here is what I want to do: if I have the string "abcde", I want it to be changed to "bcdea" (the first character is moved to the end). Here is how I did it in python:

def rotate(s):
    return s[1:] + s[:1]

I am not sure how to do this in cpp. Maybe use an array of characters?

+5
source share
8 answers

I recommend std::rotate:

std::rotate(s.begin(), s.begin() + 1, s.end());
+25
source

Here's a solution that “floats” the first character to the end of the line, sort of like a single iteration of sorting bubbles.

#include <algorithm>

string rotate(string s) {
  for (int i = 1; i < s.size(); i++)
    swap(s[i-1], s[i]);
  return s;
}

if you want the function to rotate the string in place:

#include <algorithm>

void rotate(string &s) {
  for (int i = 1; i < s.size(); i++)
    swap(s[i-1], s[i]);
}
+9

:

void RotateStringInPlace(char buffer[])
{
    // Get the length of the string.

    int len  = strlen(buffer);
    if (len == 0) {
        return;
    }

    // Save the first character, it going to be overwritten.

    char tmp = buffer[0];

    //  Slide the rest of the string over by one position.

    memmove(&buffer[0], &buffer[1], len - 1);

    // Put the character we saved from the front of the string in place.

    buffer[len - 1] = tmp;
    return;
}

, .

+5

rotate.
, :

#include <iostream>
#include <string>

std::string rotate_string( std::string s ) {
    char first = s[0];

    s.assign(s, 1, s.size() - 1);
    s.append(1, first);

    return s;
}

int main() {
    std::string foo("abcde");

    std::cout << foo << "\t" << rotate_string(foo) <<  std::endl;

    return 0;
}

, , .

: . ! № 2: , rotate_string 0. std:: out_of_range. try/catch std:: rotate: -)

+5

?

, , , , char, char .

+1

, Python ++ , , - ++.

s[1:] --> s.substr(1);
s[:1] --> s[0]; // s[0] is a char not a string, but that good enough

,

std::string rotate(const std::string &s) {
    if (s.size() > 0) return s.substr(1) + s[0];
    return s;
}

: , . , reserve append, .

+1

-

"" , , . :).

Initial Call:
rotate_about(rots_g, 0, i, j - 2);


void rotate_about(char *str, int start, int pivot, int end)
{
    if(pivot == start)
    {
        return ;
    }
    else if((pivot - start) <= (end - pivot))
    {
        int move_bytes = pivot-start;
        swap_bytes(&str[start], &str[pivot],move_bytes);
        rotate_about(str,pivot,pivot+move_bytes,end);
    }
    else
    {
        int move_bytes = end - pivot + 1;
        swap_bytes(&str[start], &str[pivot],move_bytes);
        rotate_about(str, start+move_bytes ,pivot,end);
    }
}
+1

C, : , , .

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;
  }

, , , . , , , , .

value = length - ((0 - value)% length): , , , . : 10 -9 , +1. -19 , . value = value% length: , , . , .

, , , . , . , . , , , , . index0 , . , . , , . J - . J . . K - , J . . K . J K, . , , J K, - , , temp1 . temp1 temp2. , , , . . J ​​ reset . .

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

+1
source

All Articles