2017-10-19 92 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