从字符串中删除标点符号
Removing punctuation from string of characters
《C++入门》一书中有一个练习(编号3.2.3),它问道:
编写一个程序,读取包括标点符号在内的字符串,并写出已读取但删除了标点符号的内容。
我试图解决它,但得到了一个错误:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cin >> "Enter a sentence :" >> s >> endl;
for (auto c : s)
if (ispunct(c))
remove punct;
cout << s << endl;
}
看看remove_if()
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string s;
getline(std::cin,s);
cout << s << endl;
s.erase (std::remove_if(s.begin (), s.end (), ispunct), s.end ());
cout << s << endl;
}
int main()
{
string s;
cin >> "Enter a sentence : " >> s >> endl;
for (auto c : s)
if (ispunct(c))
remove punct;
cout << s << endl;
}
您的第一个问题是您使用cin
的方式错误。cin
用于标准输入,因此尝试打印字符串和换行符是没有意义的。这是cout
:的作业
cout << "Enter a sentence : ";
cin >> s;
cout << endl;
另一个问题是remove punct
作为一个语句对编译器没有任何意义。这是一个语法错误。由于您希望打印不带标点符号的字符串,因此仅当ispunct()
返回false时才打印:
for (auto c : s)
{
if (!ispunct(c)) {
cout << c;
}
}
还要记住使用大括号以避免歧义。
相关文章:
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 在 c++ 中打开文件、删除标点符号并追加到另一个文件中
- 删除标点符号和空间的问题
- C 删除某些符号/标点符号
- 如何删除字符串中的标点符号
- 在 c++ 中删除标点符号和空格的最佳方法是什么
- C++ 从字符串中删除标点符号
- 如何删除句子中的所有标点符号
- 使用ispunct()删除标点符号
- 从字符串中删除标点符号
- 从字符数组中删除空白和标点符号的函数
- 编写一个程序,读取一串包含标点符号的字符,并写入被读取的内容,但删除了标点符号
- 删除字符串c++开头和结尾的标点符号
- C++删除字符串上的标点符号,擦除()/迭代器问题