Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

a program to check if strings are rotations of each other or not

#include<iostream>
#include<string>
using namespace std;

bool rRotation(string str1, string str2)
{
    int len1= str1.length(), len2 = str2.length();
    int j=0,i;
    for(i=0; i<len1; i++)
    {
        if(str1[i]==str2[j])
        {
            break;
        }
    }
    while(j<len2)
    {
        i = i % str1.length();
        if(str1[i] == str2[j])
        {
            i++;
            j++;
        }
        else
        {
            //cout<<"not Rotation"<<endl;
            return false;
        }
    }
    //cout<<"Rotation"<<endl;
    return true;
}

int main()
{
    string str1, str2;
    cin>>str1;
    cin>>str2;
    //rRotation(str1, str2);
    cout<<rRotation(str1, str2);
    return 0;
}
/*
ABCD
BCDA
1
*/
 
PREVIOUS NEXT
Tagged: #program #check #strings #rotations
ADD COMMENT
Topic
Name
1+2 =