A Program to check if strings are rotations of each other or not

Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1 using only one call to strstr routine?
(eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false)

Algorithm: areRotations(str1, str2)

1. Create a temp string and store concatenation of str1 to
       str1 in temp.
                          temp = str1.str1
2. If str2 is a substring of temp then str1 and str2 are
       rotations of each other.

                     str1 = "ABACD"
                     str2 = "CDABA"

     temp = str1.str1 = "ABACDABACD"
     Since str2 is a substring of temp, str1 and str2 are
     rotations of each other.


/* Function checks if passed strings (str1 and str2)
   are rotations of each other */
int areRotations(char *str1, char *str2)
  int size1   = strlen(str1);
  int size2   = strlen(str2);
  char *temp;
  void *ptr;

  /* Check if sizes of two strings are same */
  if(size1 != size2)
     return 0;

  /* Create a temp string with value str1.str1 */
  temp   = (char *)malloc(sizeof(char)*size1*2 + 1);
  temp[0] = '\0';
  strcat(temp, str1);
  strcat(temp, str1);

  /* Now check if str2 is a substring of temp */
  ptr = strstr(temp, str2);

  /* strstr returns NULL if the second string is NOT a
    substring of first string */
  if(ptr != NULL)
    return 1;
    return 0;

/* Driver program to test areRotations */
int main()
    char *str1 = "ABCD";
    char *str2 = "ABCDA";

    if(areRotations(str1, str2))
       printf("Strings are rotations of each other");
       printf("Strings are not rotations of each other");

    return 0;

Leave a Reply

Your email address will not be published. Required fields are marked *

For Inserting code :
Paste your code in the comment form, select it and then click the language link

C | C++ | Java |