C++ program to count the occurrences of a word in a string

In this article, you will learn and get code to count occurrences of a word in a string in C++. Here is the list of programs available:

Determine the Frequency of a Word in a String

This program counts the occurrence of a word in a given string. But this program is best suited to find the frequency of a sub-string (without space) in a string (entered by the user). I'll tell you about the reason later on.

The question is: write a program in C++ that finds the frequency of a word (a sub-string) in a string. Here is its answer:

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    char str[200], word[20];
    int i=0, j, temp, countW=0, chk;
    cout<<"Enter the String: ";
    gets(str);
    cout<<"Enter the Word: ";
    gets(word);
    while(str[i]!='\0')
    {
        temp = i;
        j=0;
        while(word[j]!='\0')
        {
            if(str[i]==word[j])
                i++;
            j++;
        }
        chk = i-temp;
        if(chk==j)
            countW++;
        i = temp;
        i++;
    }
    cout<<"\nOccurrence of '"<<word<<"' = "<<countW;
    cout<<endl;
    return 0;
}

This program was built and runs under the Code::Blocks IDE. Here is its sample run:

C++ count occurrence of word in string

Now supply any string, say this was fresherearth and this is fresherearth, and then a word, say fresherearth to find the frequency or occurrence of the word fresherearth in the string. Here is the final snapshot of the sample run:

find frequency of word in string c++

The dry run of the above program with user inputs of "this was fresherearth and this is fresherearth" as a string and "fresherearth" as a word goes like this:

Modified Version of the Previous Program

If your only goal is to count the occurrences of a word (without regard for the entire word) in a given string, the previous program is correct; otherwise, if you want to find the frequency of a single word in a string, the above program has a limitation, which is listed below.

Important: The above program has the limitation that it increments the value of countW if the entered word appears in any other word in the string. For example, if the string is "this is fresherearth" and the entered word is "is," Therefore, this also gets counted.

Here is the snapshot of the sample run (of the previous program) that clarifies what I mean to say:

find frequency of substring in string c++

So to improve the previous program, here is the modified version of that program:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    char str[100], word[20];
    int i, j, ls, lw, k, countWord=0, chk, doIncrement;
    cout<<"Enter the String: ";
    gets(str);
    cout<<"Enter the Word: ";
    gets(word);
    ls = strlen(str);
    lw = strlen(word);
    for(i=0; i<ls; i++)
    {
        k = i;
        doIncrement = 0;
        for(j=0; j<lw; j++)
        {
            if(str[i]==word[j])
            {
                if(k>0 && (k+lw)<ls)
                {
                    if(str[k-1]== ' ' && str[k+lw]==' ')
                        doIncrement=1;
                }
                else if(k==0 && (k+lw)<ls)
                {
                    if(str[k+lw]==' ')
                        doIncrement=1;
                }
                else if(k>0 && (k+lw)==ls)
                {
                    if(str[k-1]== ' ')
                        doIncrement=1;
                }
                if(doIncrement==1)
                    i++;
                else
                    break;
            }
        }
        chk = i-k;
        if(chk==lw)
            countWord++;
        i = k;
    }
    cout<<"\nOccurrence of '"<<word<<"' = "<<countWord;
    cout<<endl;
    return 0;
}

Here is its sample run:

c++ count word in string

C++ Quiz


« Previous Program Next Program »