Tuesday, November 22, 2011

Java Palindrome Source Code with Recursive Algorithm

//copyrighted by topsourcecode.com

import javax.swing.JOptionPane;

public class polindrome2
{
public static void main(String[]args)
{

String testWord=JOptionPane.showInputDialog("Enter Word minimum 2 letters");
boolean result=testpalindrome(testWord);

if (result==true){
System.out.println("\n**THIS WORD IS PALINDROME**");
}
else{
System.out.println("\n**THIS WORD IS NOT PALINDROME**");
}
}
//END OF MAIN METHOD
//Palindrome check method
public static boolean testpalindrome(String input)
{
int length=input.length();

if (input.charAt(0)==input.charAt(length-1)){
String newWord=input.substring(1,(length-1));

//Checking whether the input character is a odd character word
if(newWord.length()==1){
return true;
}
//Checking whether the input String is a even character word
if (newWord.length()==0){
return true;
}
//calling the recursive method
testpalindrome(newWord);
return true;
}
else{
return false;
}
}
}
Facebook