如何忽略空格和标点符号
How to ignore whitespace and punctuation?
我写了一个回文检查器函数,它在大多数情况下都有效,但如果空格或标点符号不在字符串中间,则说明它不是回文。
即
第一次测试:
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.annah
或hann 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
相关文章:
- 2D数组来自文本输入,中间有空格
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 如何在C++中读取空格分隔的输入 当我们不知道输入的数量时
- 如何在c++中使用system()来运行包含空格的python脚本
- basic_string的前导/尾部不区分空格的特征
- 移除前面有空格的前2个字符串
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 为什么 c++(g++) 不允许模板返回类型和函数名称之间有空格?
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 在字符串中插入空格
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- C++为一串单词添加空格
- 如何忽略空格和标点符号
- 如何从用户那里获得输入字符串而不是示例,然后计算空格、标点符号、数字和字母.C++
- 在 c++ 中删除标点符号和空格的最佳方法是什么
- 如何输入包含少于1000个单词的带有空格和标点符号的文本
- C++用空格和标点符号分隔字符串
- 如何标记(单词)将标点符号分类为空格