C++ String Rotation

This program accepts a string argument and returns an array of strings, which are shifted versions of the argument string.



* Featured post submitted and created by Padmanabhan.





 C++


#include <iostream>
using namespace std;

void StringRotator(string s, int len);

int main() {
    string s;
    
    //Getting Input
        
    cin>>s;
    if(s. length()!=0)
    {
        cout<<"[";
        StringRotator(s,s.length());
        cout<<"]";
    }
    else
        cout<<"Sorry! No input is given";
    return 0;
}

void StringRotator(string s,int len)
{    
    static int i=0;
    cout<<"\"";
    for(int j=i;j<len;++j)
    {
        cout<<s.at(j);
    }
    for(int k=0;k<i;++k)
    {
        cout<<s.at(k);
    }
    cout<<"\"";
    i++;
    if(i<len)
    {
        cout<<endl;
        StringRotator(s,len);
    }
}

Click here to see this code run



More Free Code Examples:


C++ Code to Reverse a Number


C++ Odd or Even


C++ Code to Reverse a String




Comments

Popular posts from this blog