Friday, July 14, 2006

Rotate one to get Another

/****************************************
Given a string s1 and a string s2,
write a snippet to say whether s2 is a
rotation of s1?

Solution:
concatenate s1 with s1 and do a search
for s2 in that. If found then s2 is a
rotation of s1.
****************************************/
int isRotation(char *s1,char *s2)
{
strcat(s1,s1);
if((temp = strstr(s1,s2))!= NULL)
{
if(strcmp(s1,temp)!= 0)
{
printf("s2 is a rotation of s1 and vice versa\n");
return 1;
}
}
return 0;
}

No comments: