/* Problem Description
Write a recursive function that takes a string, S, as input and prints the characters of S in reverse order.
Problem Constraints
1 <= |s| <= 1000 */
import java.util.*;
public class Main {
// Function to reverse and print a string using recursion
public static void reverseAndPrint(String S) {
// BASE CONDITION: If the string is empty, return
if (S.length() == 0) {
return;
}
// Recursive call with the substring starting from the second character
reverseAndPrint(S.substring(1));
// Print the first character
System.out.print(S.charAt(0));
}
public static void main(String[] args) {
// Input from the user
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
// Call the function to reverse and print the string
reverseAndPrint(str);
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: