Write a Java Program to Check whether string is palindrome or not using Function

By | March 11, 2017

Java Program to Check string palindrome

Write a Java Program to Check whether input string palindrome or not. Below I have shared a program that will check a string palindrome in java or not.

In this program I am reversing the string and then comparing the reversed string with original string. I have used Scanner class to read the string from keyboard.

Procedure : :

  • A string is taken as an input to the program. The length of the string is calculated using the string method length()and is stored in the variable n.
  • An empty string variable reverse is declared and the last character of the given string is appended to the reverse variable using the string method charAt().
  • In this process, the reverse of the given string is stored in the reverse variable. The reversed string is compared with the given string using the string method equals().
  • If the reversed string equals the given string then the given string can be considered as a palindrome otherwise it is not a palindrome.
Palindrome: :
radar, madam, mom, dad , etc.
Not Palindrome: :
Codezclub, java, road, car,  etc.

Here you will get program for string palindrome in java.The Program below is successfully compiled and run on the Windows System to produce desired output.So,Let’s check out the program below.




Now we shall write a program and get a detailed explanation after this.
SN Variables Type Description
1. sc Scanner object Instantiation of Scanner class for I/O operations. The scanner class exists in the java.util package which needs to be imported before class declaration
2. str String This will hold the value generated through keyboard
3. reverse String It will hold the final reversed string
4. len int The length of string generated through keyboard
5. main() function This function is the entry point of the program.

If you find any difficulty to understand the program then you can ask your queries in the comment section.

Leave a Reply