簡體   English   中英

java - 如何使用遞歸使回文程序忽略java中字符串所有位置的特殊字符?

[英]How to make palindrome program using recursion ignore special characters in all places of the string in java?

我的回文程序有一個輕微的邏輯錯誤,當我在字符串的前面或結尾插入特殊字符時,我得到一個指示,表明該字符串不是回文。 我正在對回文進行編程,以便在考慮字符串時忽略所有特殊字符。 例如,@bob 將被視為不是回文,而 b@ob 將被視為回文。 無論位置位於何處,我將如何編輯我的代碼以忽略特殊字符? 所有這些都是通過遞歸完成的。

'''

導入 java.util.Scanner;

公共類遞歸練習{

//the recursive function that checks to see whether the 
//string is a palindrone or not
public static boolean checkPalindrome(String str, int firstChar, int lastChar) {
    //if only one character exists
    if (firstChar == lastChar) 
        return true;
    
    //checks to see if the first and last characters match
    if ((str.charAt(firstChar)) != (str.charAt(lastChar))) 
        
        return false;
    
    //checks to see if it has characters
    if (!isChar(str))
        return true;
    
    
    //checks the middle strings with multiple characters
    //on whether or not they are a palindrome with recursive method
    if (firstChar < lastChar + 1)
        return checkPalindrome(str, firstChar + 1, lastChar - 1 );
                return true;
    
}
   //method that actually determines what a palindrome is
public static boolean isAPalindrome(String str) {
    
    int n = str.length();
    //if string is not 0 or 1 then it's a palindrome
    if(n == 0 || n == 1) 
        return false;
    return checkPalindrome(str, 0, n - 1);
    
}

//method that checks for characters
public static boolean isChar(String str) {
    
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        if (!Character.isLetter(c) && !Character.isDigit(c))
            return false;
    }
    return true;
}

//tests out recursive methods
public static void main(String args[]) {
    
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter a string to see if it's a palindrome:");
    
    String str = scanner.nextLine(); //input from the user
    
    
    //checks to see if it's a palindrome and puts them all
    //to be lower case to ignore the case issue
    if(isAPalindrome(str.toLowerCase()))
        System.out.println(str+" is a palindrome");
    
    else
        System.out.println(str+" is not a palindrome");
    
    scanner.close();
}

}

'''

這對我來說太棒了!! 我現在剛剛在學校解決這個問題,我正在網上尋找一些幫助。 那里沒有運氣!! 檢查 1 個單詞是否是回文我沒有任何問題,但是當談到句子時,我很掙扎。 所以我必須想辦法擺脫字符串中的空格,但同時保持原始字符串的完整性。 我嘗試了很多方法,但這是它對我有用的唯一方法,並且通過了所有電子書的測試。 希望這可以幫助!!!

import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        String userText;
        String backText = "";
        String userTextNoSpace = "";
        int i;

        userText = scnr.nextLine();

        if (userText.contains(" ")) {
            userTextNoSpace = userText.replace(" ", "");
            for (i = userTextNoSpace.length() - 1; i >= 0; --i) {
                backText += userTextNoSpace.charAt(i);
            }
        } else {
            for (i = userText.length() - 1; i >= 0; --i) {
                backText += userText.charAt(i);
            }
        }
        if (backText.equalsIgnoreCase(userTextNoSpace) || backText.equalsIgnoreCase(userText)) {
            System.out.println( userText + " is a palindrome");
        } else {
            System.out.println(userText + " is not a palindrome");
        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM