Java Program to Reverse a Sentence Using Recursion
What is Recursion in java.
A programming approach known as recursion involves a function calling itself to solve a problem. Recursion is a tool that Java programmers use to find solutions to problems that may be divided into smaller, related subproblems.
A base case and a recursive step are the two primary components of a recursive function. The base case, which must be specified to prevent an infinite loop, is the circumstance in which the recursion ends. The calculation in the function is the recursive phase, and it entails invoking the function once more with an altered argument that gets closer to the base case.
To know more about Java Program to Reverse a sentence using Recursion read the complete article.
How to implement Recursion in Java
Recursion in Java can be implemented by defining a method that calls itself with a modified input until a base case is reached.
- Identify the base case(s) –
- Determine the simplest possible inputs for which the answer is already known.
- Return the known answer for the base case.
- Define the recursive case
- For inputs that are not a base case, call the same method with a modified input.
- The modification should bring the input closer to the base case.
- Implement the method
- Combine the base case and the recursive case in a single method.
- Make sure the method ends by reaching the base case.
Let’s look at a Java program where the implementation of Quicksort Algorithm on java is used to perform an operation.
Example: Implementation of Recursion in Java.
public class Main { public static void main(String[] args) { String sentence = "Java program to reverse a sentence using recursion"; String reversed = reverse(sentence); System.out.println("The reversed sentence is: " + reversed); } public static String reverse(String sentence) { if (sentence.isEmpty()) return sentence; return reverse(sentence.substring(1)) + sentence.charAt(0); } }
Output
The reversed sentence is: noisrucer gnisu ecnetnes a esrever ot margorp avaJ
Example 2 : Implementation of Recursion in Java.
public class Main { public static String reverseString(String sentence) { int length = sentence.length(); if (length <= 1) { return sentence; } return reverseString(sentence.substring(1)) + sentence.charAt(0); } public static void main(String[] args) { String sentence = "PrepInsta and PrepInsta Prime "; System.out.println("The reversed sentence is: " + reverseString(sentence)); } }
Output
The reversed sentence is: emirP atsnIperP dna atsnIperP
Example 3: how to use Recursion to reverse a Sentence:
public class Main { public static String reverseString(String sentence) { int length = sentence.length(); if (length <= 1) { return sentence; } return reverseString(sentence.substring(1)) + sentence.charAt(0); } public static void main(String[] args) { String sentence = "Hello World"; System.out.println("The reversed sentence is: " + reverseString(sentence)); } }
Output
The reversed sentence is: dlroW olleH
Here’s how the recursion works in this example:
- The reverseString method is called with the argument “Hello World”.
- The first call to reverseString checks the base case, which is length <= 1. Since length is greater than 1, the method continues to the recursive step.
- The recursive step calls the reverseString method with the substring of sentence starting from the second character as the argument. In this case, the argument is “ello World”.
- The second call to reverseString checks the base case, which is length <= 1. Since length is greater than 1, the method continues to the recursive step.
- The recursive step calls the reverseString method with the substring of sentence starting from the second character as the argument. This process continues until length <= 1.
- When length <= 1, the base case returns the original sentence.
- The intermediate results are combined and the final result of the reversed sentence, “dlroW olleH”, is returned.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment