Skip to content

Instantly share code, notes, and snippets.

@madan712
Created January 28, 2014 08:27
Show Gist options
  • Select an option

  • Save madan712/8663970 to your computer and use it in GitHub Desktop.

Select an option

Save madan712/8663970 to your computer and use it in GitHub Desktop.
Java program to reverse a string using recursion
public class StringReversal {
static String reverse = "";
public static String reverse(String str) {
if (str.length() == 1) {
return str;
} else {
reverse += str.charAt(str.length() - 1)
+ reverse(str.substring(0, str.length() - 1));
return reverse;
}
}
public static void main(String[] args) {
System.out.println(reverse("Hello World!"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment