![]() |
|
|
| 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); |
|
| There is no answer at this time. |
|
| 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 |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |