File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "recursion" Watch "recursion" New topic


Nazma Panjwani

Joined: Jan 17, 2010
Posts: 17

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?

John de Michele

Joined: Mar 09, 2009
Posts: 600

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.

Nazma Panjwani

Joined: Jan 17, 2010
Posts: 17

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

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

Joined: Jul 08, 2003
Posts: 24189

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

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

Joined: Jul 08, 2003
Posts: 24189

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

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 .]

SCJP - Here for Knowledge..
Consider Paul's rocket mass heater.
subject: recursion
It's not a secret anymore!