This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes recursion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "recursion" Watch "recursion" New topic
Author

recursion

Nazma Panjwani
Greenhorn

Joined: Jan 17, 2010
Posts: 17
Hi,

I am trying to write a recursive method for determining if a string is palindromic. I have the code as follows:

String S = "radar";
int left = 0;
int right = S.length()-1;


public static void palindrome (int left, int right)
{

if (left < right || left==right)
{

if (S.charAt(left)==(S.charAt(right)))

palindrome (left+1, right-1);
}

}

I tried setting the return type to boolean, in order to indicate if the string is palindromic, but the method kept returning true (regardless). Recursion is such
a hard concept to grasp, can someone add some code to this method so it would return true depending on whether the string is palindromic, and hence
be able to print using println method?

Thanks
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nazma:

Java Ranch isn't a code mill, so don't expect someone to just fill in the answer for you. That said, I'm not sure recursion is the best technique for finding the solution. Is this an assignment, or are you just trying to figure this out? Also, there is a really simple way of finding palindromes that involves using StringBuilder.

John.
Nazma Panjwani
Greenhorn

Joined: Jan 17, 2010
Posts: 17
John,

I don't come to Java Ranch to get my assignments done. I am doing this problem just out of curiosity and trying to reinforce the concept of Recursion, a concept that I find difficult to understand. Hence, I specifically decided to do this problem using recursion, but eventually lost my patience trying to figure out the solution.

Thanks for your help


Nazma
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I think you need to try to look at from a different point of view: I think I have an approach to the problem that uses recursion and is a little easier than what you are trying.

A palindrome is a word that is the same forwards as backwards right? So you can use a recursive method to reverse the string. Then you just have to use the String class methods to see if the Strings are equal; if they are then it's a palindrome, otherwise it isn't.


Hope this helps,
Hunter M.


"If the facts don't fit the theory, get new facts" --Albert Einstein
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The most important thing to remember when you are writing a recursive method is that it must always look like this:



In other words, you must always test for the case where the recursion should stop, and do something special if it should. In your first method, the recursion should stop if "left" and "right" refer to adjacent letters, right? If they do, then if they're equal, then the two-letter word is a palindrome; if they're not, then it's not. For a longer String, if the letters are equal, you have to call the method recursively; if they're unequal, then you can return false.

So code that up!


[Jess in Action][AskingGoodQuestions]
Dennis Thomas
Greenhorn

Joined: Sep 07, 2009
Posts: 7
I agree with Hunter and his approach. Recursion can be used for reversing the string



after this you can equate both the strings and check if they are same.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Dennis Thomas wrote:I agree with Hunter and his approach. Recursion can be used for reversing the string


It could -- but the code you've shown is not recursive, it's just iterative. A recursive String-reverse function would look like

Ritesh Pareek
Ranch Hand

Joined: Nov 04, 2008
Posts: 50


@Nazma, You are not using standard flow of recursion... consider if(cond...) part and parameter modification.
Dhan Kumar
Greenhorn

Joined: Aug 03, 2009
Posts: 29

Try this ... It should work.
Test result is also in the last.

[ EJFH -- Removed giveaway answer. Dhan Kumar, please Google the "Socratic Method". The Ranch is NotACodeMill .]


Dhan
SCJP - Here for Knowledge..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: recursion
 
Similar Threads
palindrome program
palindrome not very proficient...
Palindrome
Palindrome
how to check whether a word is a palindrome