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. It is really a nice example when you need to sort
View Tutorial          By: Ranjan at 2010-10-04 23:14:33

2. Its an excellent example to learn this concept!!
View Tutorial          By: Koustav Routh at 2009-12-18 11:19:03

3. Thanks alot that was quite helpful!
View Tutorial          By: Doaa at 2013-01-09 09:37:35

4. Nearly perfect help, but the path I had to use was
View Tutorial          By: Martin Zeltner at 2009-09-15 04:15:19

5. i think i can use it in my project in IT.
<

View Tutorial          By: jamesmae amado at 2013-11-17 13:42:58

6. Nice explanation. And a good example to back it up
View Tutorial          By: fazil at 2009-07-29 00:32:03

7. i hav d same error as sudhir has "INVALID PO
View Tutorial          By: JAY SHAH at 2011-02-14 09:33:12

8. Thanx a Lot .Your Soln provided a great help
View Tutorial          By: Amit at 2011-11-21 18:35:56

9. could you please send me a complete appointment re
View Tutorial          By: Jaylon aron at 2010-02-21 02:22:57

10. very useful...thank you....
View Tutorial          By: jiby at 2012-01-19 06:40:42