Results 1 to 5 of 5
  1. #1
    thirdage is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Post 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.
    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;
    
    	}
    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!!

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default Re: Palindrome checker

    Pseudocode:
    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 endpos
    Consider using a recursive approach. Maybe a boolean isPalindrome(char[] myCharArray, int startpos, int endpos) method.
    Last edited by gcalvin; 11-06-2011 at 11:16 AM.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default Re: Palindrome checker

    You could define two small methods:

    Java 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
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    thirdage is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Palindrome checker

    I didn't learn the recursive approach yet ... for now i must do this in loops and if statements!

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default 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

  1. Sudoku checker
    By kennemercollege in forum New To Java
    Replies: 3
    Last Post: 04-27-2011, 12:41 PM
  2. number checker?
    By linc186 in forum New To Java
    Replies: 3
    Last Post: 03-05-2011, 11:12 AM
  3. isbn checker
    By jingliu316 in forum New To Java
    Replies: 2
    Last Post: 10-27-2010, 07:31 PM
  4. Grammar Checker
    By zaz_rin in forum New To Java
    Replies: 1
    Last Post: 07-19-2009, 02:01 PM
  5. ISBN Checker
    By blobs86 in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 11:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •