Check String Rotation
This program checks if one string is rotation of another.
Problem Statement
Write a Java program to check if "waterbottle" is rotation of "erbottlewat".
Source Code
| 1 | public class StringRotationCheck { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq String Rotation Check"); |
| 4 | String s1="waterbottle", s2="erbottlewat"; |
| 5 | if(s1.length()==s2.length() && (s1+s1).contains(s2)) |
| 6 | System.out.println("String is rotation"); |
| 7 | else System.out.println("String is not rotation"); |
| 8 | } |
| 9 | } |
Program Output
Eduinq String Rotation Check String is rotation
Explanation
Concatenate string with itself. - Check if second string is substring. - Together demonstrates rotation check. Conclusion: Shows how to check if string is rotation of another.