Created
June 28, 2012 18:34
-
-
Save lexich/3013105 to your computer and use it in GitHub Desktop.
Reverse string without buffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StringReverce{ | |
/** | |
* Reverse string without buffer | |
*/ | |
public static byte[] reverse(byte[] str){ | |
int end = str.length - 1; | |
for( int i = 0; i < end - i; ++i ){ | |
str[i] = (byte)( str[i] ^ str[end-i] ); | |
str[end-i] = (byte)( str[end-i] ^ str[i] ); | |
str[i] = (byte)(str[i] ^ str[end-i]); | |
} | |
return str; | |
} | |
private static void println(String pred, byte[] data){ | |
System.out.print(pred); | |
for( byte b : data ){ | |
System.out.print((char)b); | |
} | |
System.out.print('\n'); | |
} | |
public static void main(String[] args){ | |
byte str[] = (args.length > 0 ? args[0] : "Simple string").getBytes(); | |
println("Input:", str); | |
str = reverse(str); | |
println("Output:", str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment