Coding Practice

Java String Reverse - Palindrome | Java HackerRank

Java String Reverse

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

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

Sample Output
madam
Yes


...Program finished with exit code 0
Press ENTER to exit console.
Source Code
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();
        String B = "";
        int i, j, length = A.length();

        for(i = length - 1, j = 0; i >= 0; i--, j++){

            B = B + A.charAt(i);
        }
        if(B.equals(A)){
            System.out.println("Yes");
        }
        else{
            System.out.println("No");
        }
        
    }
}
Sample Output
java
No


...Program finished with exit code 0
Press ENTER to exit console.


No comments:

Post a Comment

Change Theme
X