String Java Programs

Java Program to check given string is Palindrome or not

In this tutorial, you will learn the writing program for how to check if a string is a palindrome in java .

How this Java program will behave?

To check String is Palindrome or not? This Program will take a String as an input. And after applying some logic it will return output as String is Palindrome or not.

For Example:

Suppose if we give input a string “abba”. This is palindrome String then our program will print “string is palindrome”.
And if we give “abcd” then our program will give “String is not palindrome”.

Java program to check whether a string is palindrome or not

import java.util.*;  

public class Main {  
	
    public static void main(String[] args) {     	
        Scanner sc= new Scanner(System.in);
        System.out.print("Please give First String : ");  
        String str= sc.nextLine();  
        StringBuilder strRev = new StringBuilder();
        strRev.append(str);
        strRev = strRev.reverse(); 
        if(str.equals(strRev.toString())){
            System.out.print("String is palindrom"); 
        }else{
            System.out.print("String is not palindrom"); 
        }
    }  
}  

Output

Please give First String : MALAYALAM
String is palindrom

About the Author: Elavarasan PK

Technical Specialist, Intersoft Data Labs