Skip to content

Instantly share code, notes, and snippets.

@mahmoudhossam
Created January 4, 2012 19:10
Show Gist options
  • Save mahmoudhossam/1561515 to your computer and use it in GitHub Desktop.
Save mahmoudhossam/1561515 to your computer and use it in GitHub Desktop.
How to reverse strings in Java.
public class Reverse {
public static void main(String[] args) {
System.out.println(reverse("Mahmoud"));
}
public static String reverse(String src) {
StringBuilder builder = new StringBuilder();
for(int i = src.length() - 1; i >= 0; i--) {
builder.append(src.charAt(i));
}
return builder.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment