Java String Reverse

  • + 0 comments

    Surelly not the best solution, but it was fun public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        char[] chars = A.toCharArray();
        int correctLength = chars.length % 2 == 0 ? chars.length/2 : chars.length/2 + 1;
        char[] reverseChars = new char[correctLength];
    
        /* Enter your code here. Print output to STDOUT. */
        for(int i = 0; i < correctLength; i++) {
            reverseChars[i] = chars[chars.length - i - 1];
        }
        if(new String(reverseChars).equals(A.substring(0, correctLength))){
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }