Java Program to check if a 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 .

 

Read This: What is String Palindrome? Write string palindrome program in C.

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:

palindrome string program in java

Leave a Comment