Java Program to Check whether a String is a 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.

advertisement
  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.

advertisement

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

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn