Check if a String is a Rotation of Another String in C

To check if a string is a rotation of another string in C, you can concatenate the original string with itself and then use the strstr function to determine if the second string is a substring of this concatenated string. If it is, then the second string is a rotation of the first.


Example 1: Using Concatenation and strstr

In this example, we will determine if one string is a rotation of another by concatenating the first string with itself. Then, we will use the strstr function to check whether the second string exists within the concatenated string.

Program:

</>
Copy
#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "ABCD";
    char str2[] = "CDAB";

    // Check if the strings have the same length
    if (strlen(str1) != strlen(str2)) {
        printf("Not a rotation");
        return 0;
    }

    // Create a temporary string by concatenating str1 with itself
    char temp[100];
    strcpy(temp, str1);
    strcat(temp, str1);

    // Check if str2 is a substring of temp using strstr
    if (strstr(temp, str2) != NULL)
        printf("String is a rotation");
    else
        printf("String is not a rotation");

    return 0;
}

Explanation:

  1. str1 and str2 are the two strings we want to compare.
  2. We first ensure that both strings have the same length using strlen(); if they differ, str2 cannot be a rotation of str1.
  3. We create a temporary string temp by copying str1 into it using strcpy() and then concatenating str1 to itself with strcat().
  4. The strstr() function checks if str2 is present in temp. If found, it indicates that str2 is a rotation of str1.

Output:

String is a rotation

Example 2: Using Manual Rotation Check

In this example, we manually verify if one string is a rotation of another by rotating the original string one character at a time and comparing it with the second string. This approach gives a clear insight into the rotation process.

Program:

</>
Copy
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool isRotation(char* str1, char* str2) {
    int len = strlen(str1);
    // If the lengths differ, str2 cannot be a rotation of str1
    if (strlen(str2) != len)
        return false;
    
    // Check each possible rotation
    for (int i = 0; i < len; i++) {
        bool match = true;
        // Compare rotated string with str2 character by character
        for (int j = 0; j < len; j++) {
            if (str1[(i + j) % len] != str2[j]) {
                match = false;
                break;
            }
        }
        if (match)
            return true;
    }
    return false;
}

int main() {
    char str1[] = "WATERBOTTLE";
    char str2[] = "ERBOTTLEWAT";

    if (isRotation(str1, str2))
        printf("String is a rotation");
    else
        printf("String is not a rotation");

    return 0;
}

Explanation:

  1. The function isRotation checks whether str2 is a rotation of str1.
  2. We first compare the lengths of str1 and str2 using strlen(). If they differ, the function returns false.
  3. A loop iterates over each possible rotation index i of str1.
  4. For each rotation, a nested loop compares each character of the rotated string with the corresponding character in str2 using the expression (i + j) % len to handle wrapping around.
  5. If a complete match is found for any rotation, the function returns true; otherwise, it returns false after all rotations are checked.

Output:

String is a rotation