Palindrome String Verification Example : console based java program

Before discussing simple string palindrome program in java , we should understand the term palindrome.
A Palindrome is a word , phrase , number or other sequence of symbols or elements  , whose meaning may be interpreted as the same way in eother forward direction or in reverse direction .


The word Palindrome was coined from the Greek roots palin (meaning again ) and dromos (meaning way or direction ) .The term was coined by Neb Johnson in the 17th century .

Its uses are found in every field like poetry , science , number , Phases , Molecular Biology , Asostics ,Music  ,Biological Structures , Computation theory etc.

Below example is recursion based . So  that it reduces the length of the code and make the program simple to read and understand .

String Palindrome Example :

 civicradarlevelrotorkayakreviverracecarreddermadammalyalam, and refer


String Palindrome Phrases Example
"Eva, can I stab bats in a cave?", "Mr. Owl ate my metal worm", "Was it a car or a cat I saw?", "A nut for a jar of tuna", "Do geese see God?", "Ma is as selfless as I am", "On a clover, if alive erupts a vast pure evil, a fire volcano." "Dammit, I'm mad!", "A Toyota's a Toyota", "Go hang a salami, I'm a lasagna hog", and "A Santa lived as a devil at NASA"



Demo :


Palindrome java based program




































String Palindrome Program Java Code



import java.util.*;
import java.lang.String;
public class Palindrome
{
    static Scanner console = new Scanner(System.in);
    
    public Palindrome()
    {
        
    }
    
    public static void main(String[] args)
    {//declare the variables
        String str,another="y";
        //start the loop
        while(another.equalsIgnoreCase("y"))
        {
            
            
            //prompt the user
            System.out.print("Enter a word to see if its a Palindrome; ");
            str= console.next();
            System.out.println();
            //
            //the answer
            
            if(isPalindrome(str))
            
            System.out.println( str + " is a palindrome");
            else
            System.out.println(str + " not a palindrome");
            
            
            System.out.print("test another(y/n)? ");
            another= console.next();
            
        }
    }
    
    public static boolean isPalindrome(String str)
    {
        return isPalindrome(str,0,str.length()-1);
        
    }
    public static boolean isPalindrome(String str,int low, int high)
    {  if(high <= low)
        return true;
        else if (str.charAt(low)!= str.charAt(high))
        return false;
        else
        return isPalindrome(str,low+1,high-1);
        
        
        
    }
}



About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry