reverse string with recursion

Run Settings
LanguageJava
Language Version
Run Command
class Main { public static void main(String[] args) { System.out.println(recStringRev("hello",0)); } public static String recStringRev(String s, int a) { if(a >= s.length()){ return ""; } char ch = s.charAt(a); String rev = recStringRev(s,a+1); return rev + ch; // reverse loop // we can use a stack to reverse // here we will try recursion } // Public method public static int reverse(int n) { return reverseHelper(n, 0); } // Recursive helper function private static int reverseHelper(int n, int rev) { // Base case if (n == 0) return rev; int last = n % 10; // get last digit rev = rev * 10 + last; // append to reversed number return reverseHelper(n / 10, rev); // recursive call } }
Editor Settings
Theme
Key bindings
Full width
Lines