Skip to content

Commit 8687e1e

Browse files
authored
Merge pull request #573 from divyaachoudharyy/new_branch
String Rotation
2 parents 7e4ddde + a5c97c8 commit 8687e1e

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/***************************************************************************
2+
3+
A Program to check if strings are rotations of each other or not.
4+
5+
***************************************************************************/
6+
7+
//SOLUTION (in C++):
8+
9+
//Suppose there are two strings s1 = "devincept" and s2 = "vinceptde",
10+
11+
//to check if s2 is rotation of s1, what we can do is, add s1 to itself
12+
13+
// Assign it to another string s3 = s1 + s1
14+
15+
//If s2 is a substring of s1, it is rotation of s1.
16+
17+
18+
19+
#include<bits/stdc++.h>
20+
21+
using namespace std;
22+
23+
int main()
24+
{
25+
string s1, s2, s3;
26+
cout<<"Enter the strings"<<endl;
27+
cin>>s1;
28+
cin>>s2;
29+
s3 = s1 + s1;
30+
if(s3.find(s2) != -1 && s1 != s2)//if s2 is not a substringof s1 then find returns -1
31+
{
32+
cout<<s2<<" is rotation of "<<s1;
33+
}
34+
else
35+
{
36+
cout<<s2<<" is not rotation of "<<s1;
37+
}
38+
return 0;
39+
}

0 commit comments

Comments
 (0)