Skip to content

Instantly share code, notes, and snippets.

@Jeffwan
Created February 7, 2014 04:20
Show Gist options
  • Save Jeffwan/8857334 to your computer and use it in GitHub Desktop.
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
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