views:

1956

answers:

4

I have the following homework assignment:

I need to create a program that recursively tests in a word or phrase is a palindrome. Here is the prompt:

A palindrome is "a word, line, verse, number, sentence, etc., reading the same backward as forward, as Madam, I'm Adam or Poor Dan is in a droop." (dictionary.com) When evaluating palindromes it is customary to ignore spaces and punctuation. You will write a program which uses recursion to determine whether a String contains a palindrome.

Your program should do the following:

  1. Prompt the user to enter a string.
  2. Use recursion to remove all non-letters from the string.
  3. Change the string to lower case
  4. Use recursion to test whether the string is a palindrome.

I am stuck on where to start with it. Can you suggest a starting point?

Any help on this would be greatly appreciated!

+2  A: 
  1. Open your text book or notes from class on what recursion is (or google it).
  2. Try to understand it. Ask questions about how it works if you really cant figure it out on your own.
  3. Once you figure it out, try to figure out how it could be used to solve your homework problem.
  4. Once you have a plan to solve it, implement it with the language your professor has asked for (looks like java in this case).
  5. Test your application with various input cases. You even gave us some.
  6. Once you are satisfied it is correct, submit your answer to your professor in a manner they are willing to accept.
  7. Go to professor's office and argue that late submissions should still be able to earn full credit because the most important thing in this class is that you get it.
  8. Repeat.
NickLarsen
This doesn't actually help the OP. He's asking for information that'll help him go through 2-4 on your list.
Ben S
Well thats certainly one way to interpret a cut and paste homework question.
NickLarsen
This answer utilises a loop instead of using recursion. -1.
jwaddell
@jwaddell Very nice.
NickLarsen
+1  A: 

I believe you generally want help and want to learn programming, this site is great for that, though just posting a question directly from homework isn't the best way to get your answer...or learn, ask specific questions after thinking about the problem and how to attack it. Hope this helps! Good luck!

Daniel
+6  A: 

I'll try to explain how to get to the solution rather than giving you a copy-paste answer.

When writing a recursive algorithm, the first thing to think about is how to stop the recursion. These are your base cases.

For example, you know a word is a palindrome if there is less than two letters (trivial case), and you know that a word can't be a palindrome if the first and last letters are different.

Once you've defined your ways out of a recursive method, think of the incremental step that's required to continue. The recursive call (or calls).

In the case of a palindrome, if the first and last letters are the same, you should strip them away and continue checking if the rest of the first-last combinations also match.

So following this, your basic algorithm will be:

public static boolean isPalindrome(final String word) {
    // Stop cases:
    // If word.length < 2 return true;
    // If first and last letters aren't the same, return false;

    // Recursive call:
    // return isPalindrome(word stripped of the first and last letters)
}

Of course, this is after you've cleaned up the String as described.

Ben S
A: 

Hi,

Could any of you guys tell me why the String word should be declared as a final?

Thanks in advance.

sanjan