2017-10-19 91 views
0

我寫了一個迴文檢查函數,它在大多數情況下都能正常工作,但是如果空格或標點符號不在字符串的中間,它就表示它不是迴文。如何忽略空白和標點符號?

初試:

Enter string to test for palindrome: 

hannah 

string is a palindrome. 

二測:

Enter string to test for palindrome: 

han nah 

string is a palindrome. 

第三次試驗:

Enter string to test for palindrome: 

hann.ah 

string is not a palindrome. 

第四個測試:

Enter string to test for palindrome: 

han.nah 

string is a palindrome. 

我想知道如果有一種方法可以忽略空格和標點符號都在一起,這樣h.annahhann ah會被認爲是迴文?

這裏是我的代碼:

void isPalindrome (string s){ 
    if(equal(s.begin(), s.begin() + s.size()/2, s.rbegin())) 
     cout << "string is a palindrome. " << endl; 
    else 
     cout << "string is not a palindrome. " << endl; 
} 

int main(){ 
    string test1; 
    cout << "Enter string to test for palindrome: " << endl; 
    getline(cin, test1); 

    isPalindrome(test1); 

    string test2; 
    cout << "Enter string to test for palindrome: " << endl; 
    getline(cin, test2); 

    isPalindrome(test2); 

    string test3; 
    cout << "Enter string to test for palindrome: " << endl; 
    getline(cin, test3); 

    isPalindrome(test3); 

    return 0; 
} 
+6

你應該在編寫函數的習慣得到像'isPalindrome'這樣的返回'bool'的方式,不是產生輸出的方式,也沒有給出其實際做法的其他指示。另外,像這樣的參數應該是'const string&s'來避免繁瑣的副本,並允許編譯器更好地優化。 – tadman

+1

您還需要做一些基本的閱讀,瞭解如何在程序變成無盡的剪切和粘貼痛苦之前編寫一個簡單的'for'循環。編寫一個循環執行三次應該很簡單,並且比你在這裏更簡潔。 – tadman

回答

2

應用過濾器之前的迴文檢查字符串。

這是一種方法。

#include <string> 
#include <iostream> 
#include <algorithm> 

void isPalindrome (std::string s){ 
    if(equal(s.begin(), s.begin() + s.size()/2, s.rbegin())) 
     std::cout << "string is a palindrome. " << std::endl; 
    else 
     std::cout << "string is not a palindrome. " << std::endl; 
} 

std::string remove_rubbish(std::string s) 
{ 
    auto is_rubbish = [](char c) 
       { 
        return std::ispunct(c) || std::isspace(c); 
       }; 

    s.erase(std::remove_if(s.begin(), 
          s.end(), 
          is_rubbish), 
      s.end()); 

    return s;  
} 

int main(){ 
    auto s= std::string("ha-n.n?a h"); 
    isPalindrome(remove_rubbish(s)); 

    return 0; 
} 
+0

謝謝,這真的有幫助 – Jeg

0

沒問題!只要定義算法equal_if未在C++標準尚未確定。:)

這裏是一個示範項目

#include <iostream> 
#include <string> 
#include <cctype> 

template <typename InputIterator1, typename InputIterator2, typename UnaryPredicate> 
bool equal_if(InputIterator1 first1, InputIterator1 last1, 
       InputIterator2 first2, InputIterator2 last2, 
       UnaryPredicate unary_predicate) 
{ 
    do 
    { 
     while (first1 != last1 && !unary_predicate(*first1)) ++first1; 
     while (first2 != last2 && !unary_predicate(*first2)) ++first2; 
    } while (first1 != last1 && first2 != last2 && *first1++ == *first2++); 

    return first1 == last1 && first2 == last2; 
} 

int main() 
{ 
    std::string s1("h.annah"); 
    std::string s2("hann ah"); 

    if (equal_if(s1.begin(), s1.end(), s1.rbegin(), s1.rend(), ::isalpha)) 
    { 
     std::cout << "The string \"" << s1 << "\" is a palindrome" << std::endl; 
    } 

    if (equal_if(s2.begin(), s2.end(), s2.rbegin(), s2.rend(), ::isalpha)) 
    { 
     std::cout << "The string \"" << s2 << "\" is a palindrome" << std::endl; 
    } 

    return 0; 
} 

它的輸出是

The string "h.annah" is a palindrome 
The string "hann ah" is a palindrome