Tuesday, October 10, 2017

Reverse A String using Recursion

public class ReverseAString {

       public static void main(String[] args) {
             ReverseAString ras = new ReverseAString();
             System.out.println(ras.reverseString("checkit"));
       }

       public String reverseString(String input) {
             return input.isEmpty() ? input : reverseString(input.substring(1)) + input.charAt(0);
       }

}

No comments:

Post a Comment