Develop a method that uses recursion to check for palindrome
Solution
import java.util.Scanner;
class PalindromeChecker
{
public static boolean isPalindrome(String s) //Recursion Method to check palindrome
{ // if length is 0 or 1 then String is palindrome
if(s.length() == 0 || s.length() == 1) // if length is 0 or 1 then String is palindrome
return true;
if(s.charAt(0) == s.charAt(s.length()-1))
/* check for first and last character of String:
* if they are same then we do the same thing for a substring
* with first and last char removed. and continue this
* until the input string completes or condition fails
* Function calling itself: Recursion
*/
return isPalindrome(s.substring(1, s.length()-1));
/* If program control reaches to this statement it means
* the String is not palindrome hence return false.
*/
return false;
}
public static void main(String[]args)
{
Scanner scanner = new Scanner(System.in); //For taking input from user
System.out.println(\"Enter the String for checking palindrome:\");
String string = scanner.nextLine();
/* If function returns true then the string is
* palindrome else not
*/
if(isPalindrome(string)) //calling recursive function isPalindrome
System.out.println(string + \" is a palindrome\");
else
System.out.println(string + \" is not a palindrome\");
}
} //end of program
************Output*************
Enter the String for checking palindrome:
abccba
abccba is a palindrome
...........Output*************
Enter the String for checking palindrome:
abc
abc is not a palindrome
*************Output*************
Note:I have written a full java code as i need to run it for checking whether its giving the correct output or not.You can find the method
ispalindrome(String s) within the above code.
This code will work for single word as you have already described that input will be a single word in your question.
Please let me know if you have any questions.

