不考虑空格、标点符号和大小写的回文程序

发布于 2024-12-09 12:07:45 字数 966 浏览 4 评论 0原文

我正在尝试创建一个回文程序,在确定字符串是否为回文时不考虑空格、标点符号以及大小写。

我如何更改此代码以执行我之前所说的操作?

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();
  }

} }

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();
  }

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆那么伤 2024-12-16 12:07:45

您当前的代码查看变量 str 并检查从左到右和从右到左读取的字符是否相同(也就是说,毕竟,回文是什么)。

目前,它对用户输入的原始字符串执行此操作。要更改此设置,请在内部 while 循环之前对变量 str 进行一些过滤。我将让您弄清楚到底要过滤什么/如何过滤,但请查看 String 类,如 indexOf()replace()substring()

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().

明媚殇 2024-12-16 12:07:45
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;
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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文