Created
January 28, 2014 08:27
-
-
Save madan712/8663970 to your computer and use it in GitHub Desktop.
Java program to reverse a string using recursion
This file contains hidden or 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
| 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