Java - Write code to reverse a char array In Place

Requirements

Write code to reverse a char array In Place

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        char[] input = new char[] { 'b', 'o', 'o', 'k', '2', 's', '.', 'c',
                'o', 'm', 'a', '1', };
        System.out/*w  w  w  .j av a2 s  .c  o m*/
                .println(java.util.Arrays.toString(reverseInPlace(input)));
    }

    public static char[] reverseInPlace(char[] input) {
        int length = input.length, last = length - 1;
        for (int i = 0; i < length / 2; i++) {
            char c = input[i];
            input[i] = input[last - i];
            input[last - i] = c;
        }
        return input;
    }
}

Related Quiz