Skip to content

Commit fc2ce20

Browse files
String Rotation
1 parent 5d89ea4 commit fc2ce20

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
10+
Suppose there are two strings s1 = "devincept" and s2 = "vinceptde",
11+
12+
to check if s2 is rotation of s1, what we can do is string result = s1 + s1
13+
14+
and see if s2 is a substring of s1.
15+
16+
17+
#include<bits/stdc++.h>
18+
19+
using namespace std;
20+
21+
int main()
22+
{
23+
string s1, s2, s3;
24+
cout<<"Enter the strings"<<endl;
25+
cin>>s1;
26+
cin>>s2;
27+
s3 = s1 + s1;
28+
if(s3.find(s2) != -1 && s1 != s2)
29+
{
30+
cout<<s2<<" is rotation of "<<s1;
31+
}
32+
else
33+
{
34+
cout<<s2<<" is not rotation of "<<s1;
35+
}
36+
return 0;
37+
}

0 commit comments

Comments
 (0)