# Thread: Watson Crick Recursion Help

1. Member
Join Date
Mar 2010
Posts
78
Rep Power
0

## Watson Crick Recursion Help

My teacher gave me a program, which i have to add and edit to. I dont understand exactly how to do this. it looks like im supposed to determine if the amount of letters in a string is less than or equal to 1 at one point. how do i determine that, becuase if(pal <= 1) is not allowed it seems.

Java Code:
```public boolean recursivePalindrome( String pal )
{
// ***** writes the body of this method *****

// Using recursion, determine if a String representing
// a DNA sequence is a Watson-Crick Complemented palindrome
// If it is, return true, otherwise return false

// We call the method animate inside the body of this method
// The call to animate is already coded below

animate( pal );

//if 1 or less characters left, is a palindrome - 1st base case
//if 1st character is complement (A-T, C-G) of last, continue recursion
//otherwise 2nd base case - not a palindrome

}```

2. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
11
I'll give you the basic palindrome algorithm, you modify it for your assignment:
Java Code:
```boolean isPalindrome(String pal) {
if(pal.length() <= 1)
return true;
if(pal.charAt(0) == pal.charAt(pal.length()-1))
return isPalidrome(pal.substring(1,pal.length()));
return false;
}```
So, first you check if you have a string of length 1 or 0, that is the base case that means the given string is a palindrome. If the strings length is greater than 1, check if the first and last letters are the same. If they are, chop off the first and last letter and continue recursion. If the first and last letter are not the same, the string is not a palindrome, in that case return false.
Last edited by m00nchile; 04-27-2010 at 05:03 AM.

3. Member
Join Date
Mar 2010
Posts
78
Rep Power
0
this is what i have. its getting the first if statement working right, but for my other one, it keeps coming up false, if it should be true.

Java Code:
```public boolean recursivePalindrome( String pal )
{

animate( pal );

if (pal.length() <= 1)
{
return true;
}
if(pal.charAt(0) == pal.charAt(pal.length() - 1))
{
return recursivePalindrome(pal.substring(1, pal.length()));
}
else
{
return false;
}

}```

4. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
11
Originally Posted by Meta
Java Code:
`  	return recursivePalindrome(pal.substring(1, pal.length()));`
This doesn't look right to me. The substring should cut off the first and last character of the string, and this seems to only cut off the first.

-Gary-

5. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
11
Yeah, you're right, it should be pal.substring(1,pal.length()-1). The mind does strange things when you go 20 hours without sleep :D

6. Originally Posted by m00nchile
Yeah, you're right, it should be pal.substring(1,pal.length()-1). The mind does strange things when you go 20 hours without sleep :D
Make that pal.length()-2 because you're chopping off 2 characters; no need to worry because those OBOEs (Off By One Errors) always bite me and hit me on a daily basis when I don't really pay attention ;-)

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•