Q:

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

belongs to collection: C String Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to find the highest frequency of a chara... >>
<< C program to find the first capital letter in a st...