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

By | 11.03.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.


SOURCE CODE : :


import java.util.*;
import java.lang.*;
 
public class String_Palindrome
{  
    public static void main(String args[])
    {
        String str;
        String reverse="";
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your String which u want to check : ");
        
        str = sc.nextLine();
        
        int len=str.length();
        
      
         for(int i=len-1;i>-1;i--)
        {
        reverse=reverse+str.charAt(i);
        }
        System.out.println("After Reversing, String is: " + reverse);
 
        if(str.equals(reverse))
        {
        System.out.println("Input String is Palindrome");
        }else{
                 System.out.println("Input String is not a Palindrome");
        }
    }
}

OUTPUT : :


Enter your String which u want to check : RADAR
After Reversing, String is: RADAR
Input String is Palindrome

EXPLANATION : :


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.

0 0 votes
Article Rating
Category: Java Programming String Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments