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

Java program to get location meta data from an image

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

Archived Comments

1. Hi all people...If i need to read a sms from the m
View Tutorial          By: Omarsi12 at 2009-09-06 17:11:15

2. the DomParserExample.java shows MAlformedURLExcept
View Tutorial          By: dipesh at 2011-12-09 09:53:46

3. how to run this code....... Please tell somebody m
View Tutorial          By: Honey at 2015-01-24 14:27:38

4. It works , i had a lot of trouble but finally , i
View Tutorial          By: Omer at 2009-06-30 06:05:17

5. Hi sir,
please send some sample code for sp

View Tutorial          By: hindu at 2013-03-21 06:00:09

6. Comment: yes i understood too:D:D perfect and brei
View Tutorial          By: eman at 2009-04-21 23:57:26

7. thanks a lot ya i got it.................
View Tutorial          By: sruthi at 2013-02-02 08:18:07

8. The sample code was all I needed to figure out how
View Tutorial          By: David at 2011-08-28 07:34:28

9. I'm a beginner, and I was told it is a good practi
View Tutorial          By: Nelson at 2009-08-17 09:13:28

10. I do, the necessaries changes to receive messages
View Tutorial          By: wodie at 2011-05-05 20:39:06