Java program to check whether a given string is a palindrome
KW.java
import java.util.Scanner;
class KW
{
    public static void main(String args[])
    {
        String s,r="";
        Scanner sc=new Scanner(System.in);
        System.out.println("———————————————————————————————————————————");
        System.out.println("Program to check string is palindrome | not");
        System.out.println("———————————————————————————————————————————");
        System.out.print("Enter the string ");
        s=sc.nextLine();
        int l=s.length();
        for(int i=l-1;i>=0;i--)
        r=r+s.charAt(i);
        if(s.equals(r))
            System.out.println("\n"+s+" is a palindrome");
        else
            System.out.println("\n"+s+" is a not palindrome");
        System.out.println("———————————————————————————————————————————");
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW ——————————————————————————————————————————— Program to check string is palindrome | not ——————————————————————————————————————————— Enter the string RADAR RADAR is a palindrome ——————————————————————————————————————————— kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW ——————————————————————————————————————————— Program to check string is palindrome | not ——————————————————————————————————————————— Enter the string WIN WIN is not a palindrome ——————————————————————————————————————————— kodingwindow@kw:~$
Advertisement