C++ C++;编写从段落中提取单词的函数
我正在编写的程序读取一个文本文件,将段落分解为单个单词,将它们与“敏感单词”列表进行比较,如果文本文件中的单词与敏感单词列表中的单词匹配,则会对其进行审查。我编写了查找每个单词开头的函数,以及一个将审查或用“@@@@@@@@”替换敏感单词列表中的单词的函数(我在本文中没有提到)。本例中的单词是包含字母数字字符的任何字符串 我遇到问题的函数是将“提取”或返回单个单词以与敏感单词列表(extractWord)进行比较的函数。此时,它只返回句子中最后一个单词的第一个字母。现在函数只返回“w”。我需要所有的单词 这是我到目前为止所拥有的C++ C++;编写从段落中提取单词的函数,c++,string,C++,String,我正在编写的程序读取一个文本文件,将段落分解为单个单词,将它们与“敏感单词”列表进行比较,如果文本文件中的单词与敏感单词列表中的单词匹配,则会对其进行审查。我编写了查找每个单词开头的函数,以及一个将审查或用“@@@@@@@@”替换敏感单词列表中的单词的函数(我在本文中没有提到)。本例中的单词是包含字母数字字符的任何字符串 我遇到问题的函数是将“提取”或返回单个单词以与敏感单词列表(extractWord)进行比较的函数。此时,它只返回句子中最后一个单词的第一个字母。现在函数只返回“w”。我需要所
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool wordBeginsAt (const std::string& message, int pos);
bool isAlphanumeric (char c); //
std::string extractWord (const std::string& fromMessage, int beginningAt);
int main()
{
string word = "I need to break these words up individually. 12345 count as words";
string newWord;
for (int i = 0; i < word.length(); ++i)
{
if (wordBeginsAt(word, i))
{
newWord = extractWord(word, i);
}
}
//cout << newWord; // testing output
return 0;
}
bool wordBeginsAt (const std::string& message, int pos)
{
if(pos==0)
{return true;}
else
if (isAlphanumeric(message[pos])==true && isAlphanumeric(message[pos- 1])==false)
{
return true;
}
else
return false;
}
bool isAlphanumeric (char c)
{
return (c >= 'A' && c <= 'Z')
|| (c >= 'a' && c <= 'z')
|| (c >= '0' && c <= '9');
}
std::string extractWord (const std::string& fromMessage, int beginningAt)
{
string targetWord= "";
targetWord = targetWord + fromMessage[beginningAt];
return targetWord;
}
<>这是一个C++问题,使用现代C++,而不是使用修饰的C代码如何?现代C++库有所有的算法和功能来实现所有这些工作:
#include <algorithm>
#include <cctype>
std::string paragraph;
// Somehow, figure out how to get your paragraph into this std::string, then:
auto b=paragraph.begin(), e=paragraph.end();
while (b != e)
{
// Find first alphanumeric character, using POSIX isalnum()
auto p=std::find_if(b, e, [](char c) { return isalnum(c); });
// Find the next non-alphanumeric chararacter
b=std::find_if(p, e, [](char c) { return !isalnum(c); });
if (isbadword(std::string(p, b)))
std::fill(p, b, '@');
}
…需要做些什么
您的家庭作业是如何稍微调整此代码,以避免在某些特定情况下使用空字符串调用isbadword()代码>用于第一个字符;你期待什么?当
是alphanumeric
时,需要某种循环。另外,wordBeginsAt
可以简化为return pos==0 | | isAlphanumeric(message[pos])&&!isAlphanumeric(消息[pos-1])代码>你熟悉stringstream吗?测试版…我不熟悉stringstream,我正在研究它。它在哪个图书馆。它是否像cin/cout或fstream一样工作?托尼D。。。WordsBeginsAt还没坏,所以我不打算修复它。我在extractWord中尝试了一个循环,但没有成功,但是extractWord在for循环中被调用,字符串的索引作为参数传入。我曾尝试将字符串NewWord用作数组并加载传入的字符,但。。。。我无法确定数组的大小。我提出这段代码是因为它没有语法错误,只是输出错误。执行targetWord=targetWord+fromMessage[Beginingat-1]代码>每次复制整个“targetWord”值。更有效的方法是执行targetWord.push_back(从message[beginingat-1])
这不是复制整个字符串,而是简单地修改现有的“targetWord”实例,使其具有额外的字符抱歉,我不熟悉算法或cctype库。。。我肯定需要仔细阅读这些。我是一个新的C++学生,学习基础知识。
#include <algorithm>
#include <cctype>
std::string paragraph;
// Somehow, figure out how to get your paragraph into this std::string, then:
auto b=paragraph.begin(), e=paragraph.end();
while (b != e)
{
// Find first alphanumeric character, using POSIX isalnum()
auto p=std::find_if(b, e, [](char c) { return isalnum(c); });
// Find the next non-alphanumeric chararacter
b=std::find_if(p, e, [](char c) { return !isalnum(c); });
if (isbadword(std::string(p, b)))
std::fill(p, b, '@');
}
bool isbadword(const std::string &s)