Create a Java program that implements a recursive function to check whether a string is a palindrome or not. A palindrome is a string that reads the same backward as forward, like the word radar.
main
isPalindrome
true
false
radar
import java.util.Scanner; public class CheckPalindromeRecursively { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter a string: "); String text = input.nextLine(); input.close(); boolean isPalindrome = isPalindrome(text); System.out.println(isPalindrome); } public static boolean isPalindrome(String text) { if (text.length() <= 1) { return true; } else { if (text.charAt(0) != text.charAt(text.length() - 1)) { return false; } else { return isPalindrome(text.substring(1, text.length() - 1)); } } } }
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.