开发者

Palindrome program that doesn't consider spaces, punctuation, and upper/lowercase

I am trying to create a Palindrome program that doesn't consider spaces, punctuation, and uppercase and lowercase when determining whether a string is a palindrome.

How can I change this code given to do what I stated earlier?

package palindrome;

import java.util.Scanner;

public class Palindrome {

   public static void main (String[] args)
 {
  String str, another = "y";
  int left, right;
  Scanner scan = new Scanner (System.in);

  while (another.equalsIgnoreCase("y")) // allows y or Y
  {
     System.out.println ("Enter a potential palindrome:");
     str = scan.nextLine();



     left = 0;
     right = str.length() - 1;

     while (str.charAt(left) == str.charAt(right) && left < right)
     {
        left++;
        right--;
     }

   开发者_运维百科  System.out.println();

     if (left < right)
        System.out.println ("That string is NOT a palindrome.");
     else
        System.out.println ("That string IS a palindrome.");

     System.out.println();
     System.out.print ("Test another palindrome (y/n)? ");
     another = scan.nextLine();
  }

} }


Your current code looks at the variable str and checks if characters are the same reading left-to-right and right-to-left (that is, after all, what a palindrome is).

It currently does that on the original string that the user typed in. To change this, before your inner while-loop, do some filtering on your variable str. I'll leave it to you to figure out exactly what/how to filter, but have a look at some useful methods in the String class, like indexOf(), replace() and substring().


The heart of your program is in following loop

while (str.charAt(left) == str.charAt(right) && left < right)
    {
        left++;
        right--;
    }


what you are doing here is to compare the characters without honoring the condition of ignoring space and punctuation here. Your logic should be such that while picking the characters for comparison (either from left or right) you will need to skip that character and move for the next character.
To make picture clearer see the following :

Input string :
inStr = Ma'lyalam

Step 1:
Since you have to ignore the case do following
inStr = inStr.toLowerCase();

Step 2:

 1. int left =0 , right = 8
 2. char chLeft, chRight
 3. chLeft = m , chRight = m 
 4. chLeft == chRight -> true, increment left and decrement right
 5. chLeft = a , chRight = a
 6. chLeft == chRight -> true, increment left and decrement right
 7. chLeft = ' , chRight = l -> Since chLeft = ' skip it, increment left so chLeft = l. Now continue like above

so the code should look like

    boolean isPlaindrome = false;
    str = str.toLowerCase();
    char chLeft, chRight;
    while (left < right)
    {
    chLeft = str.charAt(left);
    chRight = str.charAt(right)
    if (chLeft == ''' || chLeft == ' ')
     {
       left++
       continue;
     }
     else if (chRight == ''' || chRight == ' ')
     {
       right--;
       continue;
     }

     if (chLeft == chRight)
    {
     left++; right--;
    }
    else
    {
    break;
    }
    }

    if (left == right)
     isPlaindrome = true;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜