Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using
only one call to isSubstring (i.e., “waterbottle” is a rotation of “erbottlewat”)
int length(char * str)
{
if (str == NULL)
return 0;
int length = 0;
char * start = str;
while (*start != '\0')
{
length ++;
start ++;
}
return length;
}
bool isRotation(char * c1, char * c2)
{
if (c1 == NULL)
return false;
if (c2 == NULL)
return true;
int len = length(c1);
char * dc1 = new char[len * 2 + 1];
for (int i = 0; i < len; i++)
{
dc1[i] = c1[i];
dc1[i + len] = c1[i];
}
dc1[len * 2] = '\0';
return isSubstring(dc1, c2);
}