# Palindrome checker

• 11-06-2011, 11:59 AM
thirdage
Palindrome checker
I want to check if a phrase is a palindrome.
For example (H ell o o l l e h)
I wanna write a method that skip spaces as it reads from left and as it reads from right.
Code:

```public static boolean PalindromeChecker(String str)         {                 boolean mismatch = false;                 int len = str.length();                 int i =0;                 int j = len -1;                 while (i<j && !mismatch )                 {                         String lowerCased = str.toLowerCase();                         if (lowerCased.charAt(i)==lowerCased.charAt(j))                         {                                 mismatch =false;                         }                         else                         {                                 mismatch = true;                         }                         i = i+1;                         j=j-1;                 }                 return mismatch;         }```
My question is where should I enter the statement that would cancel spaces as i go from left and as i go from the right?
and is it an if statement .. or another loop?
Thanks a lot in advance!!
• 11-06-2011, 12:13 PM
gcalvin
Re: Palindrome checker
Pseudocode:
Code:

```    int startpos = 0     while (char at startpos is not a letter)         increment startpos     int endpos = mystring.length     while (char at endpos is not a letter)         decrement endpos```
Consider using a recursive approach. Maybe a boolean isPalindrome(char[] myCharArray, int startpos, int endpos) method.
• 11-06-2011, 12:19 PM
JosAH
Re: Palindrome checker
You could define two small methods:

Code:

```int decrement(String, s, int i) { ... } int increment(String s, int i) { ... }```
... that decrement or increment index value i until s[i] isn't a white space character or until i is an out of bounds index; if it is out of bounds the methods return -1 to signal that event. In your loop you increment i and decrement j until one of them is -1 or until i >= j.

kind regards,

Jos
• 11-06-2011, 12:29 PM
thirdage
Re: Palindrome checker
I didn't learn the recursive approach yet ... for now i must do this in loops and if statements!
• 11-06-2011, 01:18 PM
gcalvin
Re: Palindrome checker
A few other things... PalindromeChecker is a good name for the class, but a bad name for this method. boolean isPalindrome(String string) would be better. Style is important, and a method name should be a verb and should start with a lower-case letter. Also you're going to confuse yourself with that variable named mismatch. Consider converting your whole string to upper case right at the beginning, and then convert that to a char[]. Then you can do:

Code:

```    while (endpos > startpos)         while (char at startpos is not a letter)             increment startpos             if (startpos == endpos) return true         while (char at endpos is not a letter)             decrement endpos             if (startpos == endpos) return true         if (char at startpos doesn't match char at endpos) return false     if loop completes return true (it means that you've met in the middle)```