Java Program to find a String is Pallindrome or not

Programs

You have been given a String “S” and you have to find and print whether this string is a palindrome or not. If yes, print “YES” (without quotes), else print “NO” (without quotes).

Input Format
The first and only line of input contains the String S. The String shall consist of lowercase English alphabets only.

Output Format
Print the required answer on a single line.

import java.io.BufferedReader;
import java.io.InputStreamReader;


class TestClass {
    public static void main(String args[] ) throws Exception {
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String S = br.readLine();
        char[] ch = new char[S.length()];

        for(int i=0;i<S.length();i++) {
            ch[i] = S.charAt(i);
        }                
        String T = "";
        for(int i=S.length()-1;i>=0;i--) {
            T+=ch[i];
        }
        if(S.equals(T)) {
            System.out.print("YES");
        } else {
            System.out.print("NO");
        }
        
    }
}

To solve this program on HackerEarth click here.

To check more programs click here.

Leave a Reply

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