如何忽略空格和标点符号?

时间:2017-10-19 16:17:47

标签: c++ string palindrome

我写了一个回文检查功能,它大部分都有用,但如果空格或标点符号不在字符串的中间,则说它不是回文。

第一次测试:

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

2 个答案:

答案 0 :(得分:3)

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

这是单向的。

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

答案 1 :(得分: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