Java Program to Check Palindrome String

In this post, you will learn to check whether a given String is Palindrome or not in Java Programming language. You will learn to code Java Program to check Palindrome String using different methods.

Before getting to the code let’s understand “What is a Palindrome String?”

What is a Palindrome String?

Palindrome String is a String when reversed is the same as the original String. For example, madam, nitin, racecar.
When madam word is reversed it still spells madam, hence it is a palindrome string.

Java Program to Check Palindrome String

Method 1 :

Read more: How to Reverse a String in Java?

import java.util.*;

public class Main{
    
    // Main 
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter string : ");
		String originalString = sc.nextLine();
		// Convert String to character array
		char ch[]=originalString.toCharArray();  

		// Reversing the String
    		String reverseString="";  
    		for(int i=ch.length-1;i>=0;i--){  
        		reverseString+=ch[i];  
    		}  
		
		//Checking Palindrome
		if(originalString.equals(reverseString)){
		    System.out.println("\n"+originalString+" is a Palindrome String ");
		}else{
		    System.out.println(""+originalString+" is Not a Palindrome String ");
		}
        
    }
    
}

Output

Enter String : madam
madam is a Palindrome String 

How Does This Program Work ?

In the above code we first input a String using Scanner class for input in Java.

// Convert String to character array
 char ch[]=originalString.toCharArray();

Then, we convert the given string into an array of characters to reverse the string.

// Reversing the String
    String reverseString="";  
    for(int i=ch.length-1;i>=0;i--){  
       reverseString+=ch[i];  
   }  

In the above code, we traversed the character array in reverse order and then concatenated each character in that order in a new String variable called reverseString.

//Checking Palindrome
if(originalString.equals(reverseString)){
    System.out.println("\n"+originalString+" is a Palindrome String ");
} else{
     System.out.println(""+originalString+" is Not a Palindrome String ");
}

Then we check if the orginalString and reverseString are equal or not using the predefined java function equals().

Method 2 : Using Stack

import java.util.*;

public class Main {

  public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        //Input the String we want to reverse
        System.out.println("Enter a String : ");
        String originalString = sc.nextLine();
    
        //Convert String into char array using toCharArray() method
        char charArray[] = originalString.toCharArray();
    
        //Declare a stack and push each character in it
        Stack<Character> stack = new Stack<>();
        
        for (int i = 0; i < originalString.length(); i++) {
          stack.add(charArray[i]);
        }
        
        String reverseString = "";
    
        // Empty the stack and concatinate the char to form string
        while (stack.empty() == false) {
          reverseString += stack.pop();
        }
    
            // Print the reversed string
            if(originalString.equals(reverseString)){
                
                System.out.println(""+originalString+" is a Palindrome String ");
                
            }else{
                
                System.out.println(""+originalString+" is Not a Palindrome String ");
           
            }
  }

}

Output 1 :

Enter String : madam
madam is a Palindrome String 

Output 2 :

Enter String : apple
apple is a Palindrome String 

How Does This Program Work ?

In the above code, we have used the Stack data structure of Java Collections to reverse the string as we own Stack stores data in a LIFO manner.

In the above code we first input a String using Scanner class for input in Java.

// Convert String to character array
 char ch[]=originalString.toCharArray();

//Declare a stack and push each character in it
  Stack<Character> stack = new Stack<>();
        
  for (int i = 0; i < originalString.length(); i++) {
     stack.add(charArray[i]);
  }

Then, we convert the given string into an array of characters to push each character in Stack.

// Empty the stack and concatinate the char to form string
        while (stack.empty() == false) {
          reverseString += stack.pop();
        }

Then we create a reverse String by concatenating the popped characters from the stack in reverseString variable.

//Checking Palindrome
if(originalString.equals(reverseString)){
    System.out.println("\n"+originalString+" is a Palindrome String ");
} else{
     System.out.println(""+originalString+" is Not a Palindrome String ");
}

Then we check if the orginalString and reverseString are equal or not using the predefined java function equals().

Conclusion

I hope after going through this post, you understand how to code Java Program to check Palindrome String.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *