
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reverse a Sentence Using Recursion in Java
In this article, we will understand how to reverse a sentence using recursion. A recursive function is a function that calls itself multiple times until a particular condition is satisfied.
A recursive function is a function that calls itself multiple times until a particular condition is satisfied.
Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
Many programming languages implement recursion by means of stacks. Generally, whenever a function (caller) calls another function (callee) or itself as callee, the caller function transfers execution control to the callee. This transfer process may also involve some data to be passed from the caller to the callee.
Below is a demonstration of the same −
Input
Suppose our input is −
Enter the sentence : Have a nice evening
Output
The desired output would be −
The reversed input is: gnineve ecin a evaH
Algorithm
Step 1 - START Step 2 - Declare two string values namely my_input and my_result Step 3 - Read the required values from the user/ define the values Step 4 - A recursive function ‘reverseString is defined which takes an string as input and returns the character at the last position. Step 5 - The function is called recursively until the value of ‘my_input’ is not an empty string. Step 6 - The recursive function is called and the value ‘my_input’ is passed to it. Store the return value Step 7 - Display the result Step 8 - Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .
import java.util.Scanner; public class Reverse { public static void main(String[] args) { String my_input, my_result; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the sentence : "); my_input = my_scanner.nextLine(); my_result = reverseString(my_input); System.out.println("The reversed input is: " + my_result); } public static String reverseString(String my_input) { if (my_input.isEmpty()) return my_input; return reverseString(my_input.substring(1)) + my_input.charAt(0); } }
Output
Required packages have been imported A reader object has been defined Enter the sentence : Have a nice evening The reversed input is: gnineve ecin a evaH
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class Reverse { public static void main(String[] args) { String my_input, my_result; my_input = "Have a nice evening"; System.out.println("The string is defined as :" +my_input); my_result = reverseString(my_input); System.out.println("The reversed input is: " + my_result); } public static String reverseString(String my_input) { if (my_input.isEmpty()) return my_input; return reverseString(my_input.substring(1)) + my_input.charAt(0); } }
Output
The string is defined as :Have a nice evening The reversed input is: gnineve ecin a evaH