Created
February 7, 2014 04:20
-
-
Save Jeffwan/8857334 to your computer and use it in GitHub Desktop.
Implement a function reverse which reverses a string. CC 150 - Arrays and Strings
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 CC150.ArraysAndString; | |
/** | |
* Implement a function reverse which reverse a null-terminated string (null-terminated only works for C/C++) | |
*/ | |
public class ReverseString { | |
public static void main(String args[]) { | |
System.out.println(reverse("hello")); | |
} | |
public static String reverse(String str) { | |
StringBuffer sb = new StringBuffer(); | |
for (int i= str.length()-1; i>=0; i--) { | |
char value = str.charAt(i); | |
sb.append(value); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment