Created
November 15, 2012 03:02
-
-
Save zac-xin/4076379 to your computer and use it in GitHub Desktop.
Reverse a string recursively
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 ReverseString { | |
| public static void main(String args[]){ | |
| String s = "reverse test 123456789"; | |
| char[] array = s.toCharArray(); | |
| reverse(array, 0, array.length - 1); | |
| System.out.println(array); | |
| } | |
| public static void reverse(char[] array, int start, int end) { | |
| if(start < end){ | |
| char tmp = array[start]; | |
| array[start] = array[end]; | |
| array[end] = tmp; | |
| reverse(array, start + 1, end - 1); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment