C++ Program to Count Occurrence of a Word in a Text File

This C++ program will peruse a word from the client and afterwards include its all-out event in a book record “my_data.txt”.

Ensure you have as of now make this content record and have some content in it. Spot this record in a

similar index where your program source document is available.

Program

#include<iostream.h>
#include<fstream.h>
#include<string.h>
 
 
int main()
{
 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;
 
}

Output

C++ Program to Count Occurrence of a Word in a Text File

Leave a Comment

error: Alert: Content is protected!!