Sunday, 8 January 2017

Write a Java program to check whether one string is rotation of another String ?

Here we learn to check weather one string is rotation of another or not. For Example suppose we have one String as "ByeJava7WelcomeJava8" and other String as "WelcomeJava8ByeJava7" then this String is rotated version. Before checking the rotation of String user must check that both strings should have same length, otherwise they are not rotated Strings.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class CheckOneStringIsRotationOfAnother {
 
 public static void chk(String s1,String s2){
  if(s1.length() != s2.length()){
   System.err.println("THESE STRINGS ARE NOT ROTATED VERSION");
  }
  else{
   String str = s1+s1;
   if(str.contains(s2)){
    System.out.println(s1+" ,THIS STRING IS ROTATION STRING");
   }
   else{
    System.err.println(s1+" ,THIS STRING IS NOT ROTATION STRING");
   }
  }
 }
 public static void main(String[] args) {
  CheckOneStringIsRotationOfAnother.chk("ByeJava7WelcomeJava8","WelcomeJava8ByeJava7");
  
 }
}

Output:

ByeJava7WelcomeJava8 ,THIS STRING IS ROTATION STRING



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment