如何忽略空格和标点符号

How to ignore whitespace and punctuation?

本文关键字:标点符号 空格 何忽略      更新时间:2023-10-16

我写了一个回文检查器函数,它在大多数情况下都有效,但如果空格或标点符号不在字符串中间,则说明它不是回文。

第一次测试:

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

在回文检查之前对字符串应用过滤器。

这是一种方法。

#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;
}   

没问题!只需定义C++标准中尚未定义的算法equal_if:)

这是一个演示程序

#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