Write a Java program that checks whether a given string is a palindrome or not. Ex: MADAM is a palindrome.

//Program to check whether the given string is palindrome or not
import java.util.*;
class Palindrome
{
            public static void main(String args[])
            {
                        String str;
                        int flag=0;
                        Scanner scr=new Scanner(System.in);
                        System.out.println(“nEnter any String:”);
                        str=scr.next();
                        int p=str.length();
                        char ch[]=str.toCharArray();
                        for(int i=p-1,j=0;i>=j;i–,j++)
                        {
                                    if(ch[j]!=ch[i])
                                    {
                                                flag=1;
                                                break;
                                    }
                        }
                        if(flag==0)
                        System.out.println(“nGiven String is palindrome”);
                        else
                        System.out.println(“nGiven String is Not a palindrome”);   
            }
}         
Output:
Enter any String:
madam

Given String is palindrome

Leave a Reply

Your email address will not be published. Required fields are marked *

Enable Notifications OK No thanks