String Rotation in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

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

bool isRotation(char str1[], char str2[]) {
    int len1 = strlen(str1), len2 = strlen(str2);
    if (len1 != len2) return false;
    char temp[200];
    strcpy(temp, str1);
    strcat(temp, str1);
    return strstr(temp, str2) != NULL;
}

int main() {
    char str1[] = "ABCD", str2[] = "CDAB";
    if (isRotation(str1, str2))
        printf("Yes, %s is a rotation of %s\n", str2, str1);
    else
        printf("No, not a rotation\n");
    return 0;
}

C Output

Input: str1 = ABCD, str2 = CDAB
Output: Yes, CDAB is a rotation of ABCD


C++ Program

#include <iostream>
using namespace std;

bool isRotation(string s1, string s2) {
    if (s1.size() != s2.size()) return false;
    string temp = s1 + s1;
    return temp.find(s2) != string::npos;
}

int main() {
    string s1 = "hello", s2 = "lohel";
    if (isRotation(s1, s2))
        cout << "Yes, " << s2 << " is a rotation of " << s1 << endl;
    else
        cout << "No, not a rotation" << endl;
    return 0;
}

C++ Output

Input: s1 = hello, s2 = lohel
Output: Yes, lohel is a rotation of hello


JAVA Program

public class Main {
    public static boolean isRotation(String s1, String s2) {
        if (s1.length() != s2.length()) return false;
        String temp = s1 + s1;
        return temp.contains(s2);
    }

    public static void main(String[] args) {
        String s1 = "rotation", s2 = "tationro";
        if (isRotation(s1, s2))
            System.out.println("Yes, " + s2 + " is a rotation of " + s1);
        else
            System.out.println("No, not a rotation");
    }
}

JAVA Output

Input: s1 = rotation, s2 = tationro
Output: Yes, tationro is a rotation of rotation


Python Program

def is_rotation(s1, s2):
    return len(s1) == len(s2) and s2 in (s1 + s1)

s1, s2 = "python", "honpyt"
if is_rotation(s1, s2):
    print(f"Yes, {s2} is a rotation of {s1}")
else:
    print("No, not a rotation")

Python Output

Input: s1 = python, s2 = honpyt
Output: Yes, honpyt is a rotation of python


In-Depth Explanation
Example
Take the string "ABCD". Rotating it left by 1 gives us "BCDA", rotating left by 2 gives us "CDAB", and rotating left by 3 gives us "DABC". See that these are all rotations of the original string. To determine if one string is a rotation of another, we just concatenate the original string with itself ("ABCDABCD") and then check if the second string is contained within it. This is because any potential rotation needs to look like a substring in this doubled string.

Real-Life Analogy
Visualize a round clock face containing digits 1 to 12. If you rotate the clock so that "3" is at the top, the order of the numbers is different, but it's the same clock. Rotating "ABCD" does not produce a new character set; it only rearranges them in a circular fashion. Rotation checking is similar to verification of whether one clock orientation is merely a rotated version of another.

Why It Matters
This is a standard problem used in coding interviews since it checks whether you can think smart and use string manipulation, searching, and other techniques instead of relying on brute force. Naïve approach would attempt rotating the string many times and comparing for equality, which is not efficient. However, the good trick of doubling the string and doing a substring search minimizes the complexity and illustrates that you are capable of thinking smart and not just typing lengthy loops.

Learning Insights
This educates you on the value of seeing patterns in issues. Rather than trying to rotate a string incrementally, you can look at the problem differently as a string search for a substring. This ability is at the heart of programming: converting problems into easier or already known ones. This also leads to the concept of string concatenation tricks, which are applicable in issues such as pattern matching, cyclic shifts, and circular queues.

Interview Relevance
Interviewers prefer this problem because it tests both optimization and logic. If you begin by responding with "I'll rotate character by character," they will try to make you think more. When you reach the concatenation + substring search hack, it indicates problem-solving maturity. This is the type of problem that separates novice and intermediate programmers.

Real-World Applications
In practical applications, rotation check on strings can be seen in data encryption, pattern matching, note processing of music, and even in network token passing systems where data loops in a cycle. For instance, in DNA sequence analysis, rotated part can be used to signify biologically meaningful patterns. In computer networks, round robin rotation models the flow of data in ring topologies.

SEO-Optimized Closing
Understanding string rotation with examples in C, C++, Java, and Python helps beginners strengthen their foundation in string manipulation and algorithmic thinking. Whether you’re preparing for coding interviews, practicing competitive programming, or working on real-world projects involving cyclic data, learning how to check if one string is a rotation of another is a powerful concept. By learning this method, you not only answer a traditional interview question but also learn the method of simplifying problems through effective tricks that are useful in numerous programming situations.