Java program to check whether string is palindrome or not

In earlier post we have discussed how to check whether a number is palindrome or not.In today’s post we shall check whether a string is palindrome or not.For this we shall use built in functions to reverse a string and then compare the reversal of string to given string.

Procedure:1. Take input from the user and store it in a variable.
2.Here we are going to call a function “reveresing” to find the reverse of the string.
3.At last we shall call the display function to show whether it is a palindrome or not.

Now we shall write a program and get a detailed explanation after this.

import java.util.*;
class finding
   String str,reverse_str="";
   int i;
     System.out.print("Enter any string:");
     Scanner in=new Scanner(;
     str = in.nextLine();
   void reversing()
      int len_str = str.length();
          reverse_str = reverse_str+str.charAt(i);
      System.out.println("Hence reverse of the string is:"+reverse_str);
    void display()
           System.out.println("Given string " + str + "  is palindrome");
           System.out.println("Given string "+ str +" is not palindrome");
class strpalin
     public static void main(String a[])
       finding ob=new finding();

Explanation:1. In the “reversing method” we are calculating the length of the given string.Then we are adding each letter of the given string to “reverse_str” variable.Each letter of the given string is obtained using “variable.charAt()” method.
For Ex: If the given string is “hello” then by using str.charAt(2) we get l as the output.Because of indexing of the string begins from 0.
3.Therefore a reverse of the given string is obtained.
4. Now we shall compare this reverse string with given string and display accurate message.
You can download the program:download


About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my PhD in Computer Science.
This entry was posted in Java and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s