How to circularly rotate a string to left by N places in C++

2 Answers

0 votes
#include <string>
#include <iostream>
 
int main()
{
    std::string str = "ExMachina";
    
    int lenstr = str.length();
    int N = 3;
  
    std::string left_rotation = str.substr(N) + str.substr(0, N) ;

    std::cout << left_rotation;
}
 
 
 
 
/*
run:
 
achinaExM
 
*/

 



answered Nov 28, 2023 by avibootz
edited Nov 28, 2023 by avibootz
0 votes
#include <string>
#include <iostream>
#include <algorithm>
  
int main()
{
    std::string str = "ExMachina";
    int N = 3;

    rotate(str.begin(), str.begin() + N, str.end());

    std::cout << str;
}
  
  
  
  
/*
run:
  
achinaExM
  
*/

 



answered Nov 28, 2023 by avibootz

Related questions

1 answer 80 views
2 answers 91 views
1 answer 73 views
1 answer 133 views
1 answer 112 views
1 answer 78 views
78 views asked Nov 28, 2023 by avibootz
...