我有这个作业问题:
用 C++ 编写一个函数来计算文本文件中是否存在单词“ do ”。
我试过的:
我尝试首先在文本文件中搜索单词“ d ”,然后搜索“ o ”(如果它紧随其后)。
#include <iostream>
#include <fstream>
using std::fstream;
using std::cout;
using std::ios;
int main()
{
char ch[10];
int count=0, a=0;
fstream f;
f.open("P.txt", ios::in);
while(!fin.eof())
{
fin.get(ch)
if (ch[a]=='d')
{
if ((a++)=='o')
count++;
}
a++;
}
cout << "the no of do's is" << count;
f.close();
}
但这个想法是完全没用的。我想不出任何其他的想法。我很想在两种情况下对此有一个提示:
1.计算独立存在的'do'这个词。
2.计算文本中任何地方出现的“do”这个词。
这是一个数据文件处理问题。