vector<string> wordstocheck;
in.open("readin.txt");
string line;
string word = "";
int linecount = 0;
while (getline(in, line))
{
//cout << line << endl;
for (int i = 0; i < line.size(); i++)
{
if(isalpha(line[i]))
{
word.push_back(tolower(line[i]));
}
else if (line[i] == ' ' || ispunct(line[i]) || line[i] == '\n')
{
wordstocheck.push_back(word);
word = "";
}
}
linecount++;
}
for (int i = 0; i < wordstocheck.size(); i++)
{
cout << wordstocheck[i] << endl;
}
system("pause");
}
上面的代码从 .txt 文件中读取以下内容:
If debugging is the
process of removing bugs.
Then programming must be the
process of putting them in.
我试图让程序识别每个单词,并将单个单词保存到向量中,然后打印出单词向量。除了第一行和第三行的两个'the'之外,它做得很好。
Output:
if
debugging
is
theprocess
of
removing
bugs
then
programming
must
be
theprocess
of
putting
them
in
Press any key to continue . . .
它并没有像我希望的那样分裂“过程”。