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. how to refresh the radiobuttons during the run tim
View Tutorial          By: Vishnupriya at 2009-02-04 03:25:24

2. How can I save the canvas edited by the user from
View Tutorial          By: pavan at 2014-10-14 08:26:55

3. very easy to understand ... a quite a good example
View Tutorial          By: Dinesh at 2012-09-22 18:19:45

4. this is really cool......
View Tutorial          By: solex at 2009-05-16 04:02:35

5. Gud one for beginners
View Tutorial          By: puneet at 2011-07-20 05:00:42

6. I want to ask, can a formula be inserted in this s
View Tutorial          By: chika at 2011-02-02 17:18:59

7. Explanation was good and easily comprehendable.
View Tutorial          By: Moin Adil at 2009-06-17 04:18:56

8. hi can you send me the complete working code for
View Tutorial          By: avanti at 2007-10-04 02:30:04

9. sample program exception handling
View Tutorial          By: priya at 2009-09-10 05:40:29

10. You are so great at helping people. ;0
View Tutorial          By: Aaton at 2015-04-24 14:06:57