Created
January 31, 2012 21:04
-
-
Save aodukha/1712897 to your computer and use it in GitHub Desktop.
Simple string reverse algorith
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
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