C++ Program to check whether one string is a rotation of another

Program to check whether one string is a rotation of another

Today in this article we will discuss how to check whether one string is a rotation of another string, We will also disscuss various methods to do the porblem .

Lets understand this with the help of example :-

• Input:-

String1= ABCDA ,String2= DAABC

• Output:- Yes

temp = string1.string1 = “ABCDAABCDA ” Since string2 is a substring of temp, string1 and string2 are rotations of each other.

Algorithm

• Take two string input from the user a and b respectively.
• Write a function checkRotation(string a,string b) that will take the two strings as parameters.
• Check if the length of both the sting is equal or not if its not equal then return false
• Initialize a temp string that will hold the concatenated string
•  checking if s2 is present in the temp string if its present initialize flag to 0.
• Compare the value of flag if its 1 means the 2nd string is present in temp otherwise return false.

C++ Code :-

#include<bits/stdc++.h>

using namespace std;

bool checkRotation(string s1, string s2) {

// checking if the length of the two string is same or not
if (s1.length() != s2.length())
return false; //returning false if strings are not equal

string temp = s1 + s1; //storing concatenated string

int n = temp.length();
int m = s2.length();

// checking if s2 is present in temp
for(int i = 0; i<n-m; i++)
{
int flag = 1, j;
for(j = 0; j < m-1; j++)
{
if(s2[j] != temp[i+j])
{
flag = 0;
break;
}
}

if(flag == 1){
return true; // return true if s2 is present in temp
}
}

return false;
}

int main() {
string a, b ;

//Take two input from the user

cout<<"Enter string 1 : ";
getline(cin,a);

cout<<"Enter string 2 : ";
getline(cin,b);

if(checkRotation(a, b))
cout<<"Given Strings are rotations of each other.";
else
cout<<"Given Strings are not rotations of each other.";
return 0;

}

Output:-

Enter string 1 : ACDA
Enter string 2 : AACD
Strings are rotations of each other
Enter first string : ABAA
Strings are not rotations of each other

Method 2

• The approach is same here we are using build in function.
• We can use build in function to check if the string 2 is present in the concatenated string.
#include<iostream>
using namespace std;

bool checkRotation(string s1, string s2) {
// checking if the length of the two string is same or not if (s1.length() != s2.length()) return false; //returning false if strings are not equal string temp = s1 + s1; //storing concatenated string in temp int n = temp.length(); int m = s2.length(); int flag = temp.find(s2); if(flag == 1){ return true; // return true if s2 is present in temp } return false; } int main() { string a, b ; cout<<"Enter string 1 : "; getline(cin,a); cout<<"Enter string 2 : "; getline(cin,b); if(checkRotation(a, b)) cout<<"Given Strings are rotations of each other."; else cout<<"Given Strings are not rotations of each other."; return 0; }

Output:-

Enter string 1 : ABCA
Enter string 2 : AABX
Given Strings are not rotations of each other.
Enter string 1 : ACDA
Enter string 2 : AACD
Given Strings are rotations of each other.