-7

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.

Community
  • 1
  • 1
Pushkar Soni
  • 141
  • 8

1 Answers1

1

The algorithm follows from what you have. Structure your while-loop like this:

while(!fin.eof()) {
    bool found = false;    
    do {
        fin.get(ch);
        found = found || ch == 'd';
    } while (ch == 'd' && fin);

    if (found && ch == 'o') {
        // boom goes the dynamite
    }
}

The purpose of the do-while is to eliminate repeating d's, so that after that loop, you simply check if the next character is o.

Note

  • In terms of typing, the type for ch should be char ch

Explained

  • while(!fin.eof())
    • Repeat the next few lines until we reach the end of the file
  • do {
    • Beginning of a do-while loop
  • fin.get(ch);
    • Read a single byte (character) from the file
  • found = found || ch == 'd';
    • Set found to true if we have already found a d or the current character is a d
  • } while (ch == 'd' && fin);
    • End of do-while. Repeat the loop until the last character read is not a d or we have reached the end of the file
  • if (found && ch == 'o') {
    • If we were able to satisfy the condition for setting found to true and the last character we read is o...
  • // boom goes the dynamite
    • then we have successfully found the word do

Sans std::ios::eof

I won't explain this next bit, but it will follow closely with what I already posted. The goal here is to protect yourself from reading an already empty file.

while(fin >> ch) {
    while(ch == 'd' && fin.get(ch)) {    
        if (ch != 'd') {
            if (ch != 'o') {
                break;
            }
            // Found it!
        }
    }
}
smac89
  • 39,374
  • 15
  • 132
  • 179
  • thank you very much but i am a kid (17') and i couldn't understand the code. – Pushkar Soni Jan 11 '17 at 17:20
  • 1
    @PushkarSoni Age has nothing to do it. I was posting on SO when I was 17, but I knew the difference between a good question and begging to have something handed to you. – Ellis Jan 11 '17 at 17:26
  • @PushkarSoni Code explained – smac89 Jan 11 '17 at 17:29
  • If you're going to suggest how to read a file, you might as well do it correctly, see [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Thomas Matthews Jan 11 '17 at 17:32
  • actually i want to count no of 'do's in the file. – Pushkar Soni Jan 11 '17 at 18:06
  • @PushkarSoni You will have to provide further explanation if you want to get anymore meaningful answers. The code I have above can be used to find the word `do` – smac89 Jan 11 '17 at 21:35
  • @smac89 exactly. but i want to count no of do's present in the file E.g. "doafdo do do.dfdo.do" then the program should give output - 6 – Pushkar Soni Jan 12 '17 at 06:29
  • @smac89 actually i wasn't able to understand found = found || ch == 'd'; but now i understood. – Pushkar Soni Jan 12 '17 at 06:33