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. thank for your article it helps us to our studies
View Tutorial          By: shane at 2010-07-21 19:12:22

2. Hi.. Everyone... i would like to send and received
View Tutorial          By: Michael at 2011-01-29 23:43:23

3. thanks for your code
could you send me the

View Tutorial          By: diana jeng at 2009-04-21 03:50:39

4. u doing good
View Tutorial          By: matsiko at 2009-04-14 08:05:00

5. Please provide web.xml for the example
View Tutorial          By: Amit at 2008-12-24 02:16:59

6. please would someone help me create a simple java
View Tutorial          By: christopher at 2012-12-05 00:32:50

7. very usefull article............ thanks to the aut
View Tutorial          By: runa at 2010-10-16 08:43:38

8. thanks! great tutorial!
View Tutorial          By: Gracia at 2013-01-10 07:58:06

9. awesome example
View Tutorial          By: jagjyot singh at 2010-01-12 20:39:04

10. How can I send parameter to crystal report?
View Tutorial          By: Mark at 2009-10-14 03:41:36