Comment on Tutorial - Palindrome String in Java By Grant Braught



Comment Added by : Kristoffer L

Comment Added at : 2012-11-21 01:51:36

Comment on Tutorial : Palindrome String in Java By Grant Braught
Why use recursion in Java, even for Strings? The stack will grow and use more time and space. If we used Scheme, ML or a language with iterative recursion (tail recursion) it might be our only choice, but not in Java. Why not just use a loop?

Something like:
boolean isPalindrome(String str){
int i = 0;
int j = str.length() - 1;
while(i < j){
if(str.charAt(i) != str.charAt(j))
return false;
i++;
j--;
}
return true;
}

And maybe add a couple of tests ala:
if(!Character.isLetter(str.charAt(i)))
i++
if(!Character.isLetter(str.charAt(j)))
j--
or just
str.replaceAll("[^\w]", "");


View Tutorial



Related Tutorials

Program using concept of byte long short and int in java

Update contents of a file within a jar file

Tomcat and httpd configured in port 8080 and 80

Java File

Java String

Count number of vowels, consonants and digits in a String in Java

Reverse a number in Java

Student marks calculation program in Java

Handling Fractions in Java

Calculate gross salary in Java

Calculate average sale of the week in Java

Vector in Java - Sample Program

MultiLevel Inheritance sample in Java

Multiple Inheritance sample in Java

Java program using Method Overriding

Archived Comments

1. Thanks, much appreciated!
Saved a great dea

View Tutorial          By: alan at 2009-08-14 01:35:01

2. Great example. To reset the initial value on readb
View Tutorial          By: Bala at 2014-07-22 04:48:34

3. error is main fuuction declaration
void mai

View Tutorial          By: T.M.Prakash.MCA. at 2012-09-20 12:26:52

4. excellent
View Tutorial          By: shamim at 2011-07-08 02:39:31

5. No it won't work...
View Tutorial          By: Arin at 2012-08-17 09:59:40

6. thanks for suppporting us
View Tutorial          By: sonia at 2010-11-29 00:46:56

7. Does not work.
View Tutorial          By: Hallo at 2012-10-25 09:50:05

8. I want to get SCJP certification. please anyone c
View Tutorial          By: Raju Salla at 2012-05-04 08:38:06

9. Hi,

Is there a variable like GroupN

View Tutorial          By: SUNNYben at 2012-04-05 09:32:14

10. A useful article. It answered my question about wh
View Tutorial          By: Kris Heidenstrom at 2011-05-28 04:15:05