Results 1 to 5 of 5
Thread: Palindrome checker
- 11-06-2011, 10:59 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 14
- Rep Power
- 0
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.
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?Java 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; }
and is it an if statement .. or another loop?
Thanks a lot in advance!!
- 11-06-2011, 11:13 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Re: Palindrome checker
Pseudocode:
Consider using a recursive approach. Maybe a boolean isPalindrome(char[] myCharArray, int startpos, int endpos) method.Java 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 endposLast edited by gcalvin; 11-06-2011 at 11:16 AM.
- 11-06-2011, 11:19 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: Palindrome checker
You could define two small methods:
... 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.Java Code:int decrement(String, s, int i) { ... } int increment(String s, int i) { ... }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-06-2011, 11:29 AM #4
Member
- Join Date
- Oct 2011
- Posts
- 14
- Rep Power
- 0
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, 12:18 PM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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:
Java 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)
Similar Threads
-
Sudoku checker
By kennemercollege in forum New To JavaReplies: 3Last Post: 04-27-2011, 12:41 PM -
number checker?
By linc186 in forum New To JavaReplies: 3Last Post: 03-05-2011, 11:12 AM -
isbn checker
By jingliu316 in forum New To JavaReplies: 2Last Post: 10-27-2010, 07:31 PM -
Grammar Checker
By zaz_rin in forum New To JavaReplies: 1Last Post: 07-19-2009, 02:01 PM -
ISBN Checker
By blobs86 in forum New To JavaReplies: 1Last Post: 03-23-2009, 11:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks