Skip to content

Instantly share code, notes, and snippets.

@bha159
Last active February 5, 2018 00:35
Show Gist options
  • Save bha159/0f8be78770166f5796267af30e02783b to your computer and use it in GitHub Desktop.
Save bha159/0f8be78770166f5796267af30e02783b to your computer and use it in GitHub Desktop.
A c++ program to calculate occurence of a word in a text file.
#include<iostream.h>
#include<fstream.h>
#include<string.h>
using namespace std;
int main()
{
ifstream fin("my_data.txt"); //opening text file
int count=0;
char ch[20],word[20];
cout<<"Enter a word to count:";
gets(word);
while(fin)
{
fin>>ch;
if(strcmp(ch,word)==0)
count++;
}
cout<<"Occurrence="<<count<<"n";
fin.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment