Java Program to check whether one String is a rotation of another.

Java 8Object Oriented ProgrammingProgramming

To find weather a string is rotation of another, concat the first string to itself twice and find weather

Example

 Live Demo

public class Sample {
   public static void main(String args[]){
      String str1 = "gala";
      String str2 = "alag";
      String s3 = str1+str1;
      if(s3.contains(str2)) {
         System.out.println("str1 is rotation of str2");
      } else {
         System.out.println("str1 is not rotation of str2");
      }
   }
}
raja
Published on 12-Feb-2018 14:34:10
Advertisements