how to check whether strings are rotated each other or not
snippet in c

how to check whether strings are rotated each other or not

user4188

     
// C program to check if two given strings are rotations of  
// each other 
# include <stdio.h> 
# include <string.h> 
# include <stdlib.h> 
  
/* 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] = ''; 
  strcat(temp, str1); 
  strcat(temp, str1); 
  
  /* Now check if str2 is a substring of temp */
  ptr = strstr(temp, str2); 
  
  free(temp); // Free dynamically allocated memory 
  
  /* strstr returns NULL if the second string is NOT a 
    substring of first string */
  if (ptr != NULL) 
    return 1; 
  else
    return 0; 
} 
  
/* Driver program to test areRotations */
int main() 
{ 
    char *str1 = "AACD"; 
    char *str2 = "ACDA"; 
  
    if (areRotations(str1, str2)) 
       printf("Strings are rotations of each other"); 
    else
       printf("Strings are not rotations of each other"); 
  
    getchar(); 
    return 0; 
} 

how to check whether strings are rotated each other or not

user268

    
// Java program to check if two given strings are rotations of  
// each other 
  
class StringRotation 
{ 
    /* Function checks if passed strings (str1 and str2) 
       are rotations of each other */
    static boolean areRotations(String str1, String str2) 
    { 
        // There lengths must be same and str2 must be  
        // a substring of str1 concatenated with str1.   
        return (str1.length() == str2.length()) && 
               ((str1 + str1).indexOf(str2) != -1); 
    } 
      
    // Driver method 
    public static void main (String[] args) 
    { 
        String str1 = "AACD"; 
        String str2 = "ACDA"; 
  
        if (areRotations(str1, str2)) 
            System.out.println("Strings are rotations of each other"); 
        else
            System.out.printf("Strings are not rotations of each other"); 
    } 
} 
// This code is contributed by  munjal  

how to check whether strings are rotated each other or not

user911

     
// C++ program to check if two given strings 
// are rotations of  each other 
# include <bits/stdc++.h> 
using namespace std; 
  
/* Function checks if passed strings (str1 
   and str2) are rotations of each other */
bool areRotations(string str1, string str2) 
{ 
   /* Check if sizes of two strings are same */
   if (str1.length() != str2.length()) 
        return false; 
  
   string temp = str1 + str1;  
  return (temp.find(str2) != string::npos); 
} 
  
/* Driver program to test areRotations */
int main() 
{ 
   string str1 = "AACD", str2 = "ACDA"; 
   if (areRotations(str1, str2)) 
     printf("Strings are rotations of each other"); 
   else
      printf("Strings are not rotations of each other"); 
   return 0; 
}