如何让回文程序不识别空白并以句点结束?

时间:2016-02-25 07:37:44

标签: java arrays whitespace palindrome period

我需要帮助我尝试查找很多方法,但似乎无法让它工作。我需要这个不识别空白区域,所以如果用户输入le vel,它应该说“是的它是一个回文”,就像它是水平//没有空格。用户输入需要以句点结束,程序不应考虑周期。如此水平。应该回归真实。

import java.util.Scanner;

public class PalindromeDemo
{
public static void main(String[] args)
{
    String phrase, answer;
    Scanner keyboard = new Scanner(System.in);
    do
    {
        System.out.println("I will determine if a string is a palindrome");
        System.out.println("Enter a word or characters and end it with a period");
        phrase = keyboard.nextLine();

        Palindrome pd = new Palindrome();
        if(pd.checkPalindrome(phrase))

                System.out.println("YES, the phrase is palindrome!");
            else
                System.out.println("NO, the phrase is NOT palindrome.");
                System.out.println();
                System.out.println("Would you like to continue? Enter yes or no");
                answer = keyboard.nextLine();
                System.out.println();
    }
    while(answer.equalsIgnoreCase("yes"));
}
}


public class Palindrome
{
public static final int MAX_CHARS = 80;

public boolean checkPalindrome(String text)
{
    char[] array = new char[80];
    int length = text.length();

    String reverseText = "";

    for(int i = length-1; i >= 0; i--)
    {
         reverseText = reverseText + text.charAt(i);
    }

    if(reverseText.equalsIgnoreCase(text))
    {
        return true;
    }

    else
    {
        return false;
    }
}
}

3 个答案:

答案 0 :(得分:1)

试试这个。输入字符串不会改变,这比上面建议的更快更有效。

public static boolean checkPalindrome(String input){
    //input always contains period in the end.
    //ignore it.
    int length = input.length()-1-1;
    int i= 0;
    int j= length;
    while(i<j){
        if(input.charAt(i) == ' '){
            i++;
        } if (input.charAt(j) == ' '){
            j--;
        }
        if(input.charAt(i) ==input.charAt(j)){
            i++;
            j--;
        }else{
            return false;
        }
    }
    return true;

}

答案 1 :(得分:0)

要删除期间,请使用.replace(".", "")。要删除前导和尾随空格,请使用.trim()。两者都应用于String对象。很容易只使用pd.checkPalindrome(phrase.replace(".", "").trim())声明现在的if

此外,

if(pd.checkPalindrome(phrase))
    System.out.println("YES, the phrase is palindrome!");
else
    System.out.println("NO, the phrase is NOT palindrome.");
    System.out.println();
    System.out.println("Would you like to continue? Enter yes or no");
    answer = keyboard.nextLine();
    System.out.println();

应该有括号以保持逻辑流动。另外,在Palindrome中,您为什么不使用char[]?其次,我认为方法checkPalindrome应该是静态的,不需要实例化。

答案 2 :(得分:0)

使用此方法:

public boolean checkPalindrome(String text) {
    // remove all whitespace from input word (do this FIRST)
    text = text.replaceAll("\\s+", "");

    // remove optional period from end of input word
    if (text.endsWith(".")) {
        text = text.substring(0, text.length() - 1);
    }

    char[] array = new char[80];
    int length = text.length();

    String reverseText = "";

    for (int i = length-1; i >= 0; i--) {
         reverseText = reverseText + text.charAt(i);
    }

    if (reverseText.equalsIgnoreCase(text)) {
        return true;
    }
    else {
        return false;
    }
}