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.....

javax.comm.NoSuchPortExcept

View Tutorial          By: RAHUL at 2010-12-28 22:59:41

2. how to show text in tab menu & when i selected
View Tutorial          By: shadah at 2012-05-05 14:14:37

3. Easy to understand.good work
View Tutorial          By: Deepika at 2013-12-13 09:33:55

4. oh u solved my one of the hardest problem through
View Tutorial          By: zehra at 2012-01-28 19:44:14

5. Very Useful tutorial for beginners.. Thanx
View Tutorial          By: Rohini at 2010-02-23 23:16:16

6. Please guys i need sms.zip. I tried SMS.RAR but al
View Tutorial          By: Waseem at 2010-03-06 15:48:40

7. You have no way of knowing when errno was last set
View Tutorial          By: s.b at 2010-05-18 20:54:24

8. hiiis
i m working on project related to SM

View Tutorial          By: Parveen at 2008-08-08 13:23:20

9. give me some logics for programs as soon as possib
View Tutorial          By: nari at 2011-12-07 18:28:03

10. Thanks..it really worked well...!!!!
View Tutorial          By: Neelanjana at 2012-09-14 06:07:06