#include #include #include #include /* Sample midterm question: The C library function strstr() 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 strstr() (i.e., “waterbottle” is a rotation of “erbottlewat”). */ int main(int argc, char ** argv) { if (argc != 3) { fprintf(stderr, "Usage: strstr string1 string2\n"); exit(1); } char * s1 = argv[1]; char * s2 = argv[2]; int len = strlen(s1); int len2 = strlen(s2); char * s1s1; if (len != len2) { printf("Not a rotation.\n"); } else { s1s1 = (char *) malloc((len+len+1) * sizeof(s1[0])); strcpy(s1s1, s1); strcat(s1s1, s1); if (strstr(s1s1, s2)) { printf("%s is a rotation of %s\n", s1, s2); } else { printf("%s is NOT a rotation of %s\n", s1, s2); } } free(s1s1); return 0; }