rotation of string

Post new topic   Reply to topic

View previous topic View next topic Go down

rotation of string

Post  Admin on Wed Aug 13, 2008 1:45 pm

you are given two string s1 and s2 and you have to tell whether string s2 is rotation of string s1 or not you have to use strstr function only once ?
example s1=abcde
s2=cdeab
here s2 is a rotation of s1
Question

Admin
Admin

Posts: 39
Join date: 2008-08-03

View user profile http://computerclub09mnnit.forumotion.net

Back to top Go down

this should work...

Post  prani on Wed Aug 13, 2008 3:21 pm

s1=s1+s1;
strstr(s1,s2) will tell whether rotation or not

prani

Posts: 15
Join date: 2008-08-12

View user profile

Back to top Go down

Re: rotation of string

Post  ish_mnnit on Wed Aug 13, 2008 5:30 pm

#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
int main()
{
char s1[100],s2[100],s[100];
cin>>s1;
cin>>s2;
strcpy(s,s1);
strcat(s1,s);
if(strstr(s1,s2)!=NULL)
cout<<"rotation";
else
cout<<"no rotation";
getch();
}
logic of this programm is that if we add any string two times it will contain all of its rotation.

ish_mnnit

Posts: 4
Join date: 2008-08-10

View user profile

Back to top Go down

View previous topic View next topic Back to top


Permissions of this forum:
You cannot reply to topics in this forum