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. where is the main class .
View Tutorial          By: rishi at 2015-10-02 10:17:17

2. @Amit Shrivastava : Dear Amit I have tried the sam
View Tutorial          By: Sanjay at 2015-05-14 19:15:51

3. The code to show recursion is ok, but the declara
View Tutorial          By: Gaurav at 2010-08-08 01:08:27

4. Just what I was looking for. Thank you.
View Tutorial          By: Babool at 2008-05-08 08:32:50

5. for ex

public class test()
{

View Tutorial          By: Anundhara at 2012-08-11 06:54:57

6. FYI, if you're using NetBeans, the input file shou
View Tutorial          By: pberg at 2013-03-28 17:00:16

7. Hi All,
I can send sms successfully from Wi

View Tutorial          By: henrynawaung at 2008-11-14 06:41:13

8. Runtime rt=Runtime.getRuntime();
String cmd

View Tutorial          By: saranya at 2014-10-30 05:36:36

9. .. nice one ..
.. aNg gLing nka2'amazing .

View Tutorial          By: ma'kHuLet at 2010-01-19 19:25:19

10. hi!
how can i add variable form object to a

View Tutorial          By: rushi at 2015-07-16 08:18:58