Monday, November 26, 2012

C++ PROGRAM TO MAKE THE FREQUENCY COUNT OF WORDS IN A GIVEN TEXT

/*A C++ PROGRAM TO MAKE THE FREQUENCY COUNT OF WORDS IN A GIVEN TEXT*/

#include<iostream.h>
#include<conio.h>
#include<string.h>
char temp[30][30];
int check(char x[],int n)
{
    for(int i=0;i<n;i++)
    {
        if(strcmp(temp[i],x)==0)
            return 0;
    }
    return 1;
}
void main()
{
    char a[100],b[20],ch,c[10][10];
    int i=0,j,n=0,k=0,count,m;
    clrscr();
    cout<<"\n Enter any string and end with $::";
    cin.get(ch);
    do
    {
        a[n]=ch;
        n++;
        cin.get(ch);
    }while(ch!='$');
    a[n]='\0';
    for(;;)
    {
        for(j=0;(a[i]!=' ')&&(a[i]!='\0');j++)
        {
            b[j]=a[i];
            i++;
        }
        b[j]='\0';
        strcpy(c[k],b);
        k++;
        if(a[i]==0)
            break;
        i++;
    }
    cout<<"\n\n------------------------------------\n";
    cout<<"\n\tword\tFrequency\n";
    cout<<"\n\n------------------------------------\n";
    for(i=0;i<k;i++)
    {
        count=1;
        m=check(c[i],i);
        for(j=i+1;j<k;j++)
        {
            if(strcmp(c[i],c[j])==0 && m==1)
                count++;
        }
        if(m==1)
        {
            strcpy(temp[i],c[i]);
            cout<<"\t"<<c[i]<<"\t"<<count<<endl;
        }
    }
    cout<<"\n\n------------------------------------\n";
    getch();
}

/*        OUTPUT
        ------

 Enter any string and end with $::he is studying in svec and he is a good boy$
------------------------------------
    word    Frequency
------------------------------------
    he        2
    is        2
    studying  2
    in        1
    svec      1
    and       1
    a         1
    good      1
    boy          1
------------------------------------
*/

1 comment:

  1. if there is more than one space between the words ???

    ReplyDelete