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. i did the same thing..but it s nt working.. :(
View Tutorial          By: dEviL at 2008-12-03 10:20:31

2. i am newly in java, i want to learn about this and
View Tutorial          By: punitha at 2012-07-21 06:16:53

3. What is the exact meaning for the symbol of java?<
View Tutorial          By: Aravind at 2013-06-24 10:52:28

4. why the code had error in status???
View Tutorial          By: matuz at 2011-11-13 11:35:34

5. This site providing excellent knowledge about stru
View Tutorial          By: Bellamkonda at 2009-11-12 02:37:44

6. werst program out put nt comming
View Tutorial          By: anu at 2012-08-02 13:09:00

7. nice
View Tutorial          By: aish at 2011-05-06 07:30:09

8. hi friends thank you very much for the code but th
View Tutorial          By: Surya at 2008-06-13 07:06:07

9. hi this is very helpful........thank you very much
View Tutorial          By: sumalatha at 2010-06-07 02:16:37

10. Thanks for this article. Now I can understand Iter
View Tutorial          By: Rajesh Kumar at 2010-12-14 03:27:25