Saturday 11 November 2017

C Program to Find the Frequency of the Word ‘the’ in a given Sentence


Code:

#include

void main()
{
    int count = 0, i, times = 0, t, h, e, space;
    char string[100];

    puts("Enter a string:");
    gets(string);
   /*   Traverse the string to count the number of characters */
    while (string[count] != '\0')
    {
        count++;
    }
    /*   Finding the frequency of the word 'the' */
    for (i = 0; i <= count - 3; i++)
    {
        t =(string[i] == 't' || string[i] == 'T');
        h =(string[i + 1] == 'h' || string[i + 1] == 'H');
        e =(string[i + 2] == 'e'|| string[i + 2] == 'E');
        space =(string[i + 3] == ' ' || string[i + 3] == '\0');
        if ((t && h && e && space) == 1)
            times++;
    }
    printf("Frequency of the word 'the' is %d\n", times);
}

Output:

Enter a string:
The gandhi jayanthi is celeberated on october 2 is the day
that he has born.
Frequency of the word 'the' is 2


More C Programs







100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...