Skip to content

Instantly share code, notes, and snippets.

@thmain
Created June 2, 2018 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/e720d97d83e675ba88998f86137b28e9 to your computer and use it in GitHub Desktop.
Save thmain/e720d97d83e675ba88998f86137b28e9 to your computer and use it in GitHub Desktop.
public class ReverseString {
public String reverse(String input){
if(input.isEmpty())
return input;
//else put the character at the beginning to the end
//make a recursive call
return reverse(input.substring(1))+input.charAt(0);
}
public static void main(String[] args) {
ReverseString r = new ReverseString();
String input = "tutorial horizon";
String reversedString = r.reverse(input);
System.out.println("Original String: " + input);
System.out.println("Reversed String: " + reversedString);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment