Here's the code that I based here <http://www.thecrazyprogrammer.com/2015/02/c-program-count-occurrence-word-text-file.html>. (new in c++)

```
#include <iostream>
#include <fstream>
#include<cstring>
using namespace std;

int main()
{
  // std::cout << "Hello World!" << std::endl;
 // return 0;

 ifstream fin("my_data.txt"); //opening text file
 int count=0;
 char ch[20],c[20];

 cout<<"Enter a word to count:";
 gets(c);

 while(fin)
 {
  fin>>ch;
  if(strcmp(ch,c)==0)
   count++;
 } 

 cout<<"Occurrence="<<count<<"n";
 fin.close(); //closing file

 return 0;

}

```

**Error in Patter Counting**

my\_data.txt has only 3 "world" in it, but as I run the program, it results to

[![enter image description here](https://i.stack.imgur.com/7nTVp.jpg)](https://i.stack.imgur.com/7nTVp.jpg)

here's the textfile's content

[![enter image description here](https://i.stack.imgur.com/TZ2Wz.jpg)](https://i.stack.imgur.com/TZ2Wz.jpg)
What could go wrong?