C Program to count the number of times the given word occurred in the given text..

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

void main()
{
    char a[200],b[100][50],w[50];
    printf("Enter the text..\n");
    gets(a);
    printf("\nEnter the word to be counted in the given text..");
    scanf("%s",w);
    int l,i=0,j=0,k=0,c=0;

    // Extracting Each word in a 2-D Array of strings...

    while(a[i]!='\0')
    {
        if(a[i]!=' ')
        {
            b[j][k]=a[i];
            k++;
            i++;
        }
        else
        {
            b[j][k]='\0';
            k=0;
            j++;
            while(a[i]==' ')
                i++;
        }
    }
    b[j][k]='\0';

    // Counting the no of times the word occurred...

    for(l=0;l<=j;l++)
    {
        if(strcmp(b[l],w)==0)
            c++;
    }

    printf("\nThe no of times the word occurred in the given text is...%d",c);
}

No comments:

Post a comment