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. When I tried to run this code, I got time out ***
View Tutorial          By: Gandhi at 2009-06-04 06:20:48

2. really really very good
thanks all
a

View Tutorial          By: bade3 at 2010-01-01 18:52:15

3. sorry i think that's a constructor...just had one
View Tutorial          By: Gautam at 2013-10-14 15:55:14

4. Thank you so much
View Tutorial          By: Daniel at 2011-02-14 22:47:05

5. At my phone number, it showing

5 re

View Tutorial          By: Zafar Iqbal at 2013-04-02 19:48:32

6. Awesome, I am Impressed with the way the example h
View Tutorial          By: Darshan at 2010-07-09 01:34:22

7. hi....I have a project, and need help please. I ne
View Tutorial          By: kelvin65 at 2009-05-25 10:03:07

8. To 25
void *memset( void *buffer, int ch, s

View Tutorial          By: Doniv at 2011-12-15 10:10:52

9. Its very usefull to clear understanding for us .Th
View Tutorial          By: vijay at 2015-07-16 09:29:40

10. its a big help!..thankz
View Tutorial          By: mezer at 2013-07-17 05:34:07