Rotate a string in c++?

前端 未结 9 616
梦毁少年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:15

    this code worked for me:

    using namespace std;
    
    string StrRotate(string &s, int nLeft)
    {
         int size = s.size();
    
         nLeft %= size;
    
         if(nLeft == 0) return s;
    
         string after = s.substr(0, nLeft);
         string before = s.substr(nLeft, size - nLeft);
    
         return before + after;
    }
    

提交回复
热议问题