codescracker
c++

C++ Program to Count Occurrence of a Word in a String



« Previous Program C++ Tutorial »


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

Find Frequency of Word in 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 substring (without space) in a string (entered by user). I'll tell you about the reason later on.

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

// C++ Program to Count Occurrence of a Word in String
// ----codescracker.com----

#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 build and run under Code::Blocks IDE. Here is its sample run:

C++ count occurrence of word in string

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

find frequency of word in string c++

The dry run of above program with user input, this was codescracker and this is codescracker as string and codescracker as word goes like:

Modified Version of Previous Program

If your only purpose is to count the occurrence of a word (without mattering the complete word) in a given string, then previous program is correct, otherwise if you wants to find frequency of a single word in a string, then above program has a limitation as given below.

Important - Above program has a limitation that it increments the value of countW if the entered word present in any other word in the string. For example, if the string is, this is codescracker and the entered word is, is. Therefore is from this also gets counted.

Here is the snapshot of sample run (of previous program) that clarifies the things 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:

// C++ Program to Count a Word in String
// ----codescracker.com----

#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

« Previous Program C++ Tutorial »