String Rotation
Check if one string is rotation of another.
IntermediateTopic: Module 4: String Programs
Java String Rotation Program
This program helps you to learn the fundamental structure and syntax of Java programming.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first string: ");
String s1 = sc.nextLine();
System.out.print("Enter second string: ");
String s2 = sc.nextLine();
if (s1.length() == s2.length() && (s1 + s1).contains(s2)) {
System.out.println("Rotation");
} else {
System.out.println("Not Rotation");
}
sc.close();
}
}Output
Enter first string: abcde Enter second string: cdeab Rotation
Understanding String Rotation
We double the first string and check if the second is a substring.
Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.