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:
#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:
str1
andstr2
are the two strings we want to compare.- We first ensure that both strings have the same length using
strlen()
; if they differ,str2
cannot be a rotation ofstr1
. - We create a temporary string
temp
by copyingstr1
into it usingstrcpy()
and then concatenatingstr1
to itself withstrcat()
. - The
strstr()
function checks ifstr2
is present intemp
. If found, it indicates thatstr2
is a rotation ofstr1
.
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:
#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:
- The function
isRotation
checks whetherstr2
is a rotation ofstr1
. - We first compare the lengths of
str1
andstr2
usingstrlen()
. If they differ, the function returnsfalse
. - A loop iterates over each possible rotation index
i
ofstr1
. - 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. - If a complete match is found for any rotation, the function returns
true
; otherwise, it returnsfalse
after all rotations are checked.
Output:
String is a rotation