Skip to content

Instantly share code, notes, and snippets.

@johnpena
Created February 14, 2011 01:14
Show Gist options
  • Save johnpena/825364 to your computer and use it in GitHub Desktop.
Save johnpena/825364 to your computer and use it in GitHub Desktop.
Find if a string is a rotation of another.
#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