I have this Homework question:
Write a function in C++ to count the presence of a word 'do' in a text file.
What I have tried:
I tried to first search the word 'd' in the text file, then search for 'o' if present just after it.
#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();
}
but this idea is completely useless. I cannot think of any other ideas. I would love to have a Hint regarding this in two scenarios:
1.count the word 'do' independently existing.
2.count the word 'do' present anywhere in the text.
this is a data file handling question.