C program to find the frequency of every word in the given string

Here, we are going to learn how to find the frequency of every word in the given string in C programming language?
Submitted by Nidhi, on July 20, 2021

Problem Solution:

Read a string from the user and find the frequency of each word in the input string using C program.

Program:

The source code to find the frequency of every word in the given string is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to find the frequency of every word 
// in the given string

#include <stdio.h>
#include <string.h>

void printFrequencyofWords(char* str)
{
    int count = 0;
    int c = 0;

    int i = 0;
    int j = 0;
    int k = 0;
    int spaceCnt = 0;

    char str1[64];

    char p[20][50];
    char ptr1[20][50];

    char* ptr;

    for (; i < strlen(str); i++) {
        if ((str[i] == ' ') || (str[i] == ',' && str[i + 1] == ' ') || (str[i] == '.')) {
            spaceCnt = spaceCnt + 1;
        }
    }

    for (i = 0; j < strlen(str); j++) {
        if ((str[j] == ' ') || (str[j] == 44) || (str[j] == 46)) {
            p[i][k] = '\0';
            i++;
            k = 0;
        }
        else
            p[i][k++] = str[j];
    }

    k = 0;
    i = 0;
    for (; i <= spaceCnt; i++) {
        for (j = 0; j <= spaceCnt; j++) {
            if (i == j) {
                strcpy(ptr1[k], p[i]);
                k = k + 1;
                count = count + 1;
                break;
            }
            else {
                if (strcmp(ptr1[j], p[i]) != 0)
                    continue;
                else
                    break;
            }
        }
    }

    for (i = 0; i < count; i++) {
        for (j = 0; j <= spaceCnt; j++) {
            if (strcmp(ptr1[i], p[j]) == 0)
                c = c + 1;
        }
        printf("Frequency of '%s' is: %d\n", ptr1[i], c);
        c = 0;
    }
}

int main()
{
    char str[64];
    printf("Enter the string: ");
    scanf(" %[^\n]s", str);

    printFrequencyofWords(str);

    return 0;
}

Output:

RUN 1:
Enter the string: this is india it is india
Frequency of 'this' is: 1
Frequency of 'is' is: 2
Frequency of 'india' is: 2
Frequency of 'it' is: 1

RUN 2:
Enter the string: is are am is are am
Frequency of 'is' is: 2
Frequency of 'are' is: 2
Frequency of 'am' is: 2

RUN 3:
Enter the string: hello, world
Frequency of 'hello' is: 1
Frequency of '' is: 1
Frequency of 'world!' is: 1

Explanation:

In the above program, we created two functions printFrequencyofWords() and main(). The printFrequencyofWords() function is used to find the frequency of each word in the string.

In the main() function, we read a string from the user and called the printFrequencyofWords() function and printed the frequency of each word in the string on the console screen.

C String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.