この宿題の質問があります:
テキスト ファイル内の単語 ' do 'の存在をカウントする関数を C++ で記述します。
私が試したこと:
最初にテキスト ファイル内の単語 ' 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();
}
しかし、この考えはまったく役に立ちません。他にアイデアが思いつきません。これに関するヒントを 2 つのシナリオで提供したいと思います。
1.独立して存在する単語「do」を数える。
2.テキストのどこかにある「do」という単語を数えます。
これはデータファイルの取り扱いに関する質問です。