C++ Program to Count Occurrence of Word using File Handling

By | 05.01.2017

Count Occurrence of Word using File Handling


Write a C++ Program to Count Occurrence of Word using File Handling. Here’s simple C++ Program to Count Occurrence of Word using File Handling in C++ Programming Language.


This C++ program will read  a word from user and then count its total occurrence in a text file “file4.txt”. Make sure you have already create this text file and have some text in it. Place this file in the same directory where your program source file is present.

Below is the source code for C++ Program to Count Occurrence of Word using File Handling which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* C++ Program to Count Occurrence of Word using File Handling  */

/*---------------file4.txt Content--------------

/ This C++ program will read  a word from user
/ and then count its total occurrence in a text
/ file “file4.txt”. Make sure you have already
/ create this text file and have some text in it.
/ Place this file in the same directory where your
/ program source file is present.

/---------------------------------------------*/


#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;

int main()
{
     cout<<"Opening the text file............\n ";
     ifstream fin("C:\\Users\\acer\\Documents\\file4.txt"); //opening text file
     cout<<"\nFile opened successfully.............\n";

     int count=0;
     char ch[20],c[20];

     cout<<"\nEnter any word which u want to count :: ";
     cin>>c;

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

     cout<<"\nOccurrence of word [ "<<c<<" ] = "<<count<<"\n";
     fin.close(); //closing file

     return 0;

}

OUTPUT : :


/* C++ Program to Count Occurrence of Word using File Handling  */

/*---------------file4.txt Content--------------

/ This C++ program will read  a word from user
/ and then count its total occurrence in a text
/ file “file4.txt”. Make sure you have already
/ create this text file and have some text in it.
/ Place this file in the same directory where your
/ program source file is present.

/---------------------------------------------*/

/--------------------------OUTPUT-------------------------------------/

Opening the text file............

File opened successfully.............

Enter any word which u want to count :: file

Occurrence of word [ file ] = 4

Process returned 0

Above is the source code for C++ Program to Count Occurrence of Word using File Handling which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

3.5 2 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
caprisun77

Hey there, great piece of code. However when i try applying it to a file of my own that i want it to read from it fails and comes up with the (11db) error. Can you help me with this???