Rotational palindrome in java


In this core java programming tutorial we will Write a program to check whether String is Rotational palindrome in java


Hi! In this post will try to figure out whether our input string is rotational palindrome or not.

Q1.What is palindrome in java?
A. If reverse of string is same as original one, than our string is palindrome or,
palindrome is a string which reads the same backward or forward.

Q2.What is rotational palindrome in java?
A. If we rotate a string and it forms a palindrome it's called rotational palindrome.

Must read: Factorial using recursion in java.  


Let’s understand it with diagrams

Our initial string is  aaaad
 
 

STEP1 in Rotational palindrome in java>Rotate the string.

String formed after rotation-
   


STEP2 in Rotational palindrome in java>
Check whether rotated string is palindrome or not.
As we see aaada is not palindrome, we will again again rotate the string.
   
String formed after rotation-
 

STEP3 in Rotational palindrome in java>
Check whether rotated string is palindrome or not.
Yes, aadaa is palindrome.
Hence, we can say aaaad is a rotational palindrome.

Q. For how long we will keep on rotating string if rotation of string not forming any palindrome in java.
A. We will keep on rotating our string until our rotated string becomes equal to input string in java.
Example> Let’s say our inputString is abcde (which is not rotational palindrome), we will rotate it 5 times(length of string) and exit in java.

Full Program/SourceCode/ Example to check whether String is Rotational palindrome in java >
/** Copyright (c), AnkitMittal www.JavaMadeSoEasy.com */
public class RotationalPalindromeExample {
   public static void main(String[] args) {
         
          String inputString;
          inputString = "aaaad";
         
          isRotationalPaliondrome(inputString);
         
   }
  
  
   /**wrapper method which calls rotateString() method, until we have checked all possible rotations of String.
   * @param inputString
   * @return true if inputString is rotation of palindrome
   */
   public static boolean isRotationalPaliondrome(String inputString){
                 char ar[]=inputString.toCharArray();
                 for(int x=0;x<inputString.length();x++){
                       rotateString(ar);
                       if(isPalindrome(ar)){
                              System.out.println("inputString(i.e "+inputString+") is rotation of the palindrome: "+String.valueOf(ar));
                              return true;
                       }
                 }
                 return false;    
   }
  
   /**
   * Method rotates the String.
   */
   public static void rotateString(char[] ar){
          char temp = ar[0];
          int x=0;
          for(x=0;x<ar.length-1;x++){
                 ar[x]=ar[x+1];
          }
          ar[x]=temp;
   }
  
   /**
   *
   */
   public static boolean isPalindrome(char ar[]){
          for(int i=0,j=ar.length-1; i<(ar.length/2); i++,j--){
         if(ar[i]!=ar[j])
                return false;
     }
     return true;
   }
  
}
/** OUTPUT
inputString(i.e aadaa) is rotation of the palindrome: aadaa
*/

We could above functionality with following rotational palindromes as well-
aaada
aaaad
daaaa
adaaa

Summary >
So in this core java programming tutorial we wrote Wrote a program how to check whether String is Rotational palindrome in java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.




Previous program                                                                  Next program




RELATED LINKS>


eEdit
Must read for you :