iSpunct()未检测到单引号字符

ispunct() does not detect single quote character

本文关键字:单引号 字符 检测 iSpunct      更新时间:2023-10-16

我正在尝试在文件中读取并从文件中删除所有标点符号。我一直在使用iSpunct()通过字符串迭代,并检查字符是否是标点符号,但似乎并没有捕获所有标点符号。我想知道我是否做错了。这是我的代码:

2.TXT

你好吗?

我很好,谢谢。

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
//removes punctuation, numbers, and extra spaces
void removeNonAlph(string &tmp)
{
     for(int i = 0; i < tmp.length(); i++)
     {
         if (ispunct(tmp[i]))
             tmp.erase(i--, 1);
         else if (isdigit(tmp[i]))
             tmp.erase(i--, 1);
         else if ((tmp[i] == ' ') && (tmp[i+1]) == ' ')
             tmp.erase(i--, 1);
     }
 }
int main(int argc, const char * argv[]) 
{
    ifstream file("2.txt");
    string tmp;
    string words[500];
    while (getline(file, tmp))
    {
        removeNonAlph(tmp);
        toLower(tmp);
        cout << tmp << endl;
    }
    file.close();
}

输出:

你好吗

我很好,谢谢

(注释移动以回答未来读者的轻松发现)

提防编辑器将非ASCII引号放入您的文本文件中。许多编辑者生成的"智能引号"通过使用不同的非ASCII角色代码渲染的右图和左引号来看起来更好。ispunct通常仅适用于ASCII输入。