Check Whether One String Is Rotation Of Another | Java Programs

Output:

*** Check Whether s1 is Rotated Version Of s2 ***

s1 is: rogrammingExercisesJavaP
s2 is: JavaProgrammingExercises

s2 is a rotated version of s1

Click Here For Java Online Compiler

Solution:

public class RotatedString {

    public static void main(String[] args) {
        System.out.println("*** Check Whether s1 is Rotated Version Of s2 ***" + "\n");
        String s1 = "rogrammingExercisesJavaP";
        String s2 = "JavaProgrammingExercises";

        System.out.println("s1 is: " + s1);
        System.out.println("s2 is: " + s2 + "\n");
        if (s1.length() != s2.length()) {
            System.out.println("s2 is not rotated version of s1");
        } else {

            String s3 = s1 + s1;

            if (s3.contains(s2)) {
                System.out.println("s2 is a rotated version of s1");
            } else {
                System.out.println("s2 is not rotated version of s1");
            }
        }
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon