How to reverse a String? How to deal with it if the input string is null?
Utilisateur anonyme
If you want to save on the extra "swap" memory, you can use bitwise operator XOR like this: public String reverse( String string ) { byte[] array = string.getBytes(); for( int i = 0, j = array.length - 1; i < array.length / 2; i++, j-- ) { array[ i ] ^= array[ j ]; array[ j ] ^= array[ i ]; array[ i ] ^= array[ j ]; } return new String( array ); }