Java Program to Check if a Given String is Palindrome

«
»
This is a Java Program to Check whether a String is a Palindrome.

Enter any string as input. Now we use for loops and if-else conditions along with equalsIgnoreCase() method to conclude whether the entered string is palindrome or not.

Here is the source code of the Java Program to Check whether a String is a Palindrome. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. public class Palindrome
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         String a, b = "";
  6.         Scanner s = new Scanner(System.in);
  7.         System.out.print("Enter the string you want to check:");
  8.         a = s.nextLine();
  9.         int n = a.length();
  10.         for(int i = n - 1; i >= 0; i--)
  11.         {
  12.             b = b + a.charAt(i);
  13.         }
  14.         if(a.equalsIgnoreCase(b))
  15.         {
  16.             System.out.println("The string is palindrome.");
  17.         }
  18.         else
  19.         {
  20.             System.out.println("The string is not palindrome.");
  21.         }
  22.     }
  23. }

Output:

$ javac Palindrome.java
$ java Palindrome
 
Enter the string you want to check:NeveroddorEVen
The string is palindrome.

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.