This C++ program will read a word from user and then count its total occurrence in a text file “my_data.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.
ifstream fin("my_data.txt"); //opening text file
cout<<"Enter a word to count:";
fin.close(); //closing file
11 thoughts on “C++ Program to Count Occurrence of a Word in a Text File”
good exposure to programming…
using namespace std;
char c = “C++ programming is not easy.”, check = ‘m’;
int count = 0;
for(int i = 0; c[i] != ‘\0’; ++i)
if(check == c[i])
cout << "Frequency of " << check << " = " << count;
Could you create program for calculate number of keywords in a file ?
why strcmp are not declared in this scope?
write a program that ask the user for the name of a file name assume the file contains a series of numbers each written on a seperate line the program should read the contents of file into an array and then display sum of these numbers
can you please solve it for me
Mr crazy programmer where is stdio.h header file ,because you are using gets function.
Where is stdio.h
So what is strcmp ? Is it compare? When i play with c or c++ i atend to write functions that already exist. I spend all the time writing the function .
This program wont work if a fullstop comes after the word that is to be searched. As full stop would come inside the string that is being compared.
My Name Is Rohan.
And word searched is rohan
Then count would appear 0.
can you tell the occurrence of each word in a file
here what does this do?