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. its show objects are not equal
View Tutorial          By: Amit at 2012-12-22 14:56:29

2. Good
View Tutorial          By: sirisha at 2009-09-08 23:04:22

3. Hi,
I am Mahesh. i want to sending sms cod

View Tutorial          By: Mahesh at 2011-05-20 05:24:22

4. hai vij i am also having error what sol u used to
View Tutorial          By: uma at 2008-05-30 01:05:39

5. Hello all..
can anyone please help me.. i

View Tutorial          By: Christoven at 2011-06-15 05:55:16

6. guys i am getting this error..plz tell me the solu
View Tutorial          By: Ajit at 2010-06-17 07:38:51

7. hello
i am developing a budget application

View Tutorial          By: anu at 2010-04-16 04:26:36

8. I have a squid proxy installed as forward proxy an
View Tutorial          By: ruffy at 2012-12-20 12:34:15

9. I would like to know how to obtain the object that
View Tutorial          By: Steve at 2012-01-18 15:58:31

10. i'm interest in j2me so i have to learn about j2me
View Tutorial          By: cha at 2013-10-14 13:17:22