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

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.

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

9 thoughts on “C++ Program to Count Occurrence of a Word in a Text File

    1. mukul


      using namespace std;
      int main()
      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;
      return 0;

  1. Rahul sharma

    Could you create program for calculate number of keywords in a file ?

  2. hamza

    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

  3. Hade

    Mr crazy programmer where is stdio.h header file ,because you are using gets function.

  4. michael

    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 .

  5. Deepanshu Jain

    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.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.