Google Answers Logo
View Question
 
Q: Java -- Write a recursive method to determine a palindrome. ( No Answer,   1 Comment )
Question  
Subject: Java -- Write a recursive method to determine a palindrome.
Category: Computers > Programming
Asked by: guruman-ga
List Price: $10.00
Posted: 25 Oct 2005 07:55 PDT
Expires: 27 Oct 2005 10:21 PDT
Question ID: 584656
Java -- Write a recursive method to determine whether a given string
is a palindrome or not.  A string is a palindrome if it reads the same
forward and backward.  Ignore the case of the letters and punctuation
marks. Demonstrate that you rmethod works by placing it in a main and
displaying the output. For example, the result of a call to your
method might be:

The word ?cow? is not a a palindrome.
OR
The word ?tot? is a palindrome.


//determine if the given string is a palindrome
//first call should have index = 0
boolean isPalindrome(String str, int index) {
int endPos = str.length() ? index ? 1;
if (index >= str.length() / 2) {
return true;
}
else if (Character.toUpperCase(str.charAt(index)) ==
Character.toUpperCase(str.charAt(endPos))) {
return isPalindrome(str, index + 1);
Answer  
There is no answer at this time.

Comments  
Subject: Re: Java -- Write a recursive method to determine a palindrome.
From: expertforyou-ga on 27 Oct 2005 08:08 PDT
 
public class Palindrome
{
    public Palindrome()
        {
        }
        public boolean isPalindrome(String s)
        {
                if (s.length() <= 1)
                        return true;    // Base case
                else
                {
                        if (s.charAt(0) == s.charAt(s.length() - 1))
                                return isPalindrome(s.substring(1,
s.length() - 1 ) );
                        else
                                return false;
                }
        } // isPalindrome()
} // Palindrome

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy