Java String Reverse : Hacker Rank Solution : Digit Wood

Java
,

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


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

Constraints

 will consist at most  lower case english letters.

Sample Input

Sample Output

SOLUTION:

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

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String x = rev(s);
        if (s.equals(x)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }

    public static String rev(String s) {
        char[] ch = s.toCharArray();
        int sp = 0;
        int ep = ch.length - 1;
        while (sp < ep) {
            char temp = ch[sp];
            ch[sp] = ch[ep];
            ch[ep] = temp;
            sp++;
            ep--;
        }
        return new String(ch);
    }
}

FOLLOW FOR MORE QUESTIONS AND SOLUTIONS | DIGIT WOOD

Leave a Reply

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