Palindrome using recursion in java


In this core java programming tutorial we will write a program to check whether string is Palindrome using recursion in java.



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

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

Example in java>
input String : aabaa
output: it’s palindrome.



Full Program/SourceCode/ Example to check whether string is Palindrome using recursion in java >
/** Copyright (c), AnkitMittal www.JavaMadeSoEasy.com */
public class PalindromeRecursionExample {
   public static void main(String...args){
      String inputString="aabaa";
      System.out.println(isPalindromeUsingRecursion(inputString) ? inputString+ " is a palindrome." :  inputString+ "is not a palindrome.");   
   }
  
   /**
   * This methods finds out whether inputString is palindrome or not recursively.
   * Returns true if inputString is palindrome.
   */
   public static boolean isPalindromeUsingRecursion(String inputString){
          if(inputString.length()==0 || inputString.length()==1){
                 return true;
          }
          if(inputString.charAt(0)==inputString.charAt(inputString.length()-1)){
                 return isPalindromeUsingRecursion(inputString.substring(1,inputString.length()-1));
          }
  
   return false;
   }
}
/*OUTPUT
aabaa is a palindrome.
*/




Summary >
So in this core java programming tutorial we wrote a program to check whether string is Palindrome using recursion 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>








Must read for you :

No comments:

Post a Comment