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”).
#include <iostream> using namespace std; bool isSubstring(string s1, string s2) { /* string::find The position of the first character of the first match. If no matches were found, the function returns string::npos. */ if(s1.find(s2) != s1.npos) return true; else return false; } bool isRotation(string s1, string s2) { if( (s1.length() == s2.length()) && s1.length() > 0) return isSubstring(s1+s1, s2); else return false; } int main(int argc, const char * argv[]) { string s1 = "abcdef"; string s2 = "cdefab"; if(isRotation(s1,s2)) cout<<"TRUE"<<endl; else cout<<"FALSE"<<endl; return 0; }