Java String Reverse Program using Recursion

Write a Java string reverse program using recursion or recursive functions. In this Java example, stringReverse checks whether the string is empty, and if it’s true, it prints the exact string. Otherwise, it performs the string reverse recursively.

package RemainingSimplePrograms;

import java.util.Scanner;

public class StringReverseRecur1 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {

		sc= new Scanner(System.in);
		
		System.out.print("Please Enter String to Reverse =  ");
		String str = sc.nextLine();
		
		String revString = stringReverse(str);	
		System.out.println("The reversed String =  " + revString);
	}
	
	public static String stringReverse(String str)
	{
		if(str.isEmpty())
			return str;
		
		return stringReverse(str.substring(1)) + str.charAt(0);
	}
}
Java String Reverse Program using Recursion

It is another way of writing string reverse programs in Java using recursion.

package RemainingSimplePrograms;

import java.util.Scanner;

public class StringReverseRecur2 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {

		sc= new Scanner(System.in);
		
		System.out.print("Please Enter String to Reverse =  ");
		String str = sc.nextLine();
		
		System.out.print("The reversed String =  " );
		stringReverse(str);	
	}
	
	public static void stringReverse(String str)
	{
		if(str.isEmpty()) {
			System.out.print(str);
		}
		else {
			System.out.print(str.charAt(str.length() - 1));
			stringReverse(str.substring(0, str.length() - 1));
		}	
	}
}
Please Enter String to Reverse =  Welcome to Java World!
The reversed String =  !dlroW avaJ ot emocleW