Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

recursion

 
Nazma Panjwani
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Dennis Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


@Nazma, You are not using standard flow of recursion... consider if(cond...) part and parameter modification.
 
Dhan Kumar
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic