Home >>Java Programs >Check for string rotation in Java
In this example, we will create a java program to check whether a given string is a rotation of another string.
public class Main
{
public static void main(String[] args)
{
String str1 = "ABCDEF", str2 = "DEFABC";
if(str1.length() != str2.length())
{
System.out.println("Second string is not a rotation of first string");
}
else
{
str1 = str1.concat(str1);
if(str1.indexOf(str2) != -1)
System.out.println("Second string is a rotation of first string");
else
System.out.println("Second string is not a rotation of first string");
}
}
}