How to circularly rotate a string to right 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 right_rotation = str.substr(lenstr - N, N) + str.substr(0, lenstr - N);

    std::cout << right_rotation;
}
 
 
 
 
/*
run:
 
inaExMach
 
*/

 



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.rbegin(), str.rbegin() + N, str.rend());

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

 



answered Nov 28, 2023 by avibootz

Related questions

1 answer 73 views
2 answers 108 views
1 answer 80 views
1 answer 132 views
1 answer 644 views
...