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



Subscribe to Tutorials

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. Hi,
i am using crystal report 9. Once i ins

View Tutorial          By: jeff at 2012-02-12 08:14:15

2. Hi,

I am using this code and gettin

View Tutorial          By: Akanksha at 2014-01-20 10:31:31

3. good one
View Tutorial          By: yogesh at 2010-07-24 21:18:24

4. Post your errors, so that others may help you my f
View Tutorial          By: yogesh at 2010-01-12 00:45:42

5. its good
View Tutorial          By: Srinu at 2011-05-26 23:57:18

6. I got a project on J2ME and i was to start to kno
View Tutorial          By: md kadir ali , vit university , vellore at 2010-05-12 00:59:58

7. its very useful......to me
View Tutorial          By: dinesh at 2014-08-26 12:47:38

8. Thanks for help
View Tutorial          By: Asha at 2014-12-20 08:25:48

9. Thanks a ton. Was struggling to get the ListView u
View Tutorial          By: Avinash at 2012-09-25 12:25:12

10. hi,
i am safeer, i am try this program in

View Tutorial          By: Safi at 2009-05-22 22:32:04