Create a Java program that prompts the user for a string and implements a recursive function to invert a string of characters.
main
java
avaj
import java.util.Scanner; public class ReverseStringRecursively { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter a string: "); String text = input.nextLine(); input.close(); String reversedText = reverse(text); System.out.println(reversedText); } public static String reverse(String text) { if (text.length() <= 1) { return text; } char firstChar = text.charAt(0); String remainingText = text.substring(1); return reverse(remainingText) + firstChar; } }
Click here to view the exercise solution
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
The code has been successfully copied to the clipboard.
Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!
Create a Java program that implements a recursive function to calculate the result of raising an integer to another integer. This function should be created using recursion.
Create a Java program that implements a recursive function called multiply() that takes two integer numbers requested from the user and returns the result of their multiplication.
Create a Java program that uses recursion to calculate a number in the Fibonacci series.
Create a Java program that implements a recursive function that receives a requested number from the user and returns the factorial of that number.
Create a Java program that requests a string from the user and implements a recursive function to reverse a string of characters.
Create a Java program that implements a recursive function to check if a string is a palindrome or not.
Practice with recursion exercises in Java. Learn to use recursive algorithms and check their efficiency.
Free programming course with practical exercises and solutions in C#. Start learning right now!
Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.