Chapter 15 # 5 PC for Starting Out With Java 1st Edition 5) Palindrome Detector
ID: 3629276 • Letter: C
Question
Chapter 15 # 5 PC for Starting Out With Java 1st Edition5) Palindrome Detector
A palindrome is any word phrase, or sentence that reads the same forward
and backward. Here are some well-known palindromes:
Albe was I, ere I saw Elba
A man, a plan, a canal, Panama
Desserts, I stressed
Kayak
Write a boolean method that uses recursion to determine if a string argument
is a palindrome. The method should return true if the argument reads the same
forward & backward. Demonstrate the method in a program.
Explanation / Answer
please rate - thanks
import java.util.*;
public class recursivepalindrone
{
public static void main(String args[])
{
Scanner in =new Scanner(System.in);
boolean palin;
String input;
System.out.print("Enter a string: ");
input=in.nextLine();
palin=isPalindrome(input,0,input.length()-1);
if(palin)
System.out.println(input+" is a Palindrome");
else
System.out.println(input+" is not a Palindrome");
}
public static boolean isPalindrome(String str,int low, int high)
{
if(high <= low)
return true;
if(Character.isDigit(str.charAt (low))||(Character.isLetter(str.charAt (low)))
&&(Character.isDigit((str.charAt (high)))||(Character.isLetter(Character.toUpperCase(str.charAt (high))))))
if( Character.toUpperCase(str.charAt (low))==Character.toUpperCase(str.charAt (high)))
return isPalindrome(str,low+1,high-1);
else return false;
else
{
if(Character.isDigit(Character.toUpperCase(str.charAt (low)))||(Character.isLetter(Character.toUpperCase(str.charAt (low)))))
return isPalindrome(str,low,high-1);
else
return isPalindrome(str,low+1,high);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.