Write a program to print reverse of a String using recursion?

 Recursion on Strings:-

  1. Write a program to print reverse of a String using recursion?

//Write a program to print reverse of a String
using recursion?
import java.util.Scanner;

public class Reverse {

static String reverse(String s,int idx){
    if(idx==s.length()) return "";
    String smallAns=reverse(s, idx+1);
    return smallAns+s.charAt(idx);
}

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    String s=sc.nextLine();
    System.out.println(reverse(s, 0));
}
   
}

Tausif

Hi! My name is TAUSIF AHMAD I have completed B.Tech in Computer Science from Maulana Azad National Urdu University Hyderabad. I am always ready to have new experiences meet new people and learn new things. 1. I am very interested in Frontend Development. 2. I love video editing and graphics designing. 3. I enjoy challenges that enables to grow. 4. I am part time Blogger.

Post a Comment (0)
Previous Post Next Post