In: Computer Science
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not.
The method must not have any loops!
In JAVA
The answer to this question is as follows:
The code is as follows:
import java.util.*;
public class Palindrome
{
static boolean isPal(String str,int start, int
end)
{
if (start == end)
return true;
if ((str.charAt(start)) !=
(str.charAt(end)))
return
false;
if (start < end + 1)
return
isPal(str, start + 1, end - 1);
return true;
}
static boolean checkPalindrome(String str)
{
int len = str.length();
if (len == 0)
return true;
return isPal(str, 0, len -
1);
}
public static void main(String args[])
{
//I am providing the input as the static
String str = "madam";
//If you want the input as the
dynamic please comment above line and uncomment below two
line
//Scanner sc=new
Scanner(System.in);
//String str=sc.nextLine();
System.out.println(checkPalindrome(str));
}
}
The input and output are provided in the screenshot below: