Created
February 14, 2011 01:14
-
-
Save johnpena/825364 to your computer and use it in GitHub Desktop.
Find if a string is a rotation of another.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
int isrotation(char*, char*); | |
main() { | |
int result = isrotation("byegood", "goodbye"); | |
if (result == 0) { | |
printf("Nope.\n"); | |
} else { | |
printf("Yup.\n"); | |
} | |
} | |
// Determines if str2 is a rotation of str1 by adding str1 to itself | |
// and checking if str2 is in the concatenation. | |
int isrotation(char* str1, char* str2) { | |
char concat[strlen(str1)*2]; /* make a new string to hold str1 + str1 */ | |
strcat(concat, str1); | |
strcat(concat, str1); | |
char* is_in = strstr(concat, str2); | |
if (is_in != NULL) { | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment