Java Palindrome String Check

By | October 24, 2019

Task: Write down a Java String Program to check for Palindrome.

"<yoastmark

The Source Code of Java Program Check for Palindrome

import java.util.Scanner;

/**
 *
 * @author www.EasyCodeBook.com
 */
public class StringPalindrome {

   
    public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
        System.out.print("Enter a String to check for Palindrome: ");
        String s1=input.nextLine();
        String s2="";
        
        for(int i=s1.length()-1;i>=0;i--)
           s2=s2+s1.charAt(i);
        
        System.out.println("Original string=" + s1);                
        System.out.println("Reverse string=" + s2);
       if(s1.equals(s2))
            System.out.println("The Given String is a Palindrome");
        else
            System.out.println("The Given String is a Not a Palindrome");
             
            
    }
    
}

Output of Java Check Palindrome Program

Enter a String to check for Palindrome: madam
Original string=madam
Reverse string=madam
The Given String is a Palindrome

Enter a String to check for Palindrome: Java Palindrome Check
Original string=Java Palindrome Check
Reverse string=kcehC emordnilaP avaJ
The Given String is a Not a Palindrome

Enter a String to check for Palindrome: MoM
Original string=MoM
Reverse string=MoM
The Given String is a Palindrome

Case Insesitive Palindrome Check

You can use String Method equalsIgnoreCase() to perform a case insensitive palindrome check.

Loading

Leave a Reply

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