Skip to content

Instantly share code, notes, and snippets.

@aodukha
Created January 31, 2012 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aodukha/1712897 to your computer and use it in GitHub Desktop.
Save aodukha/1712897 to your computer and use it in GitHub Desktop.
Simple string reverse algorith
package gl.sample.test;
public class StringReversSample {
public static String reverseString(String src){
if(src == null || src.isEmpty())
return src;
int strlen = src.length();
char buffer[] = new char[src.length()];
src.getChars(0, strlen, buffer, 0);
int replaceIndx = strlen-1;
char symblBuff;
for(int indx=0; indx < replaceIndx; indx++,replaceIndx--){
symblBuff = buffer[replaceIndx];
buffer[replaceIndx] = buffer[indx];
buffer[indx] = symblBuff;
}
return new String(buffer);
}
public static void main(String[] args) {
String testMe = "Hello world";
String testResult = reverseString(testMe);
System.out.println(testResult);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment