Interview Questions, Answers and Tutorials

Java String Reverse

Java String Reverse

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.


Given a string A, print Yes if it is a palindrome, print No otherwise.

Constraints

  • A will consist at most 50 lower case english letters.

Sample Input

madam

Sample Output

Yes

Solution:

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        sc.close();
        String B = "";
        
        for(int i=A.length()-1;i>=0;i--){
            B = B.concat(""+A.charAt(i));
        }
        
        if(A.equals(B)){
            System.out.print("Yes");
        }
        else{
            System.out.print("No");
        }
        
    }
}