Q:

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

belongs to collection: C++ File Handling Solved Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program to Read Write Student Details using Fi... >>
<< C++ Program to read text file and write in another...