KEYWORDS.C


#include 
#include 

typedef struct _keyword
{    char* word;
     int num;
} keyword;

keyword keywords[]={ {"break",0},
                     {"continue",0},
                     {"float",0},
                     {"for",0},
                     {"if",0},
                     {"return",0},
                     {"struct",0},
                     {"while",0}
                   };


int getword(char word[], int lim)
{   int c, i=0;
    while(!isalpha(c=getchar()) && c!=EOF)
               ;
    if (c==EOF)
        return -1;
    do
    {  word[i++]=c;
    } while(--lim>0 && isalpha(c=getchar()));
 
    word[i]='\0';
    return i;
}


int cmp(const void* a, const void* b)
{       return strcmp((char*)a, (*(keyword*)b).word);
}


main()
{   char word[80];
    int i;
    int num_of_keywords=sizeof(keywords)/sizeof(keyword);

    while (getword(word,80)!=-1)
    {        keyword* k=(keyword*)bsearch((void*)word,(void*)keywords,
                   num_of_keywords,sizeof(keyword),&cmp);
       if (k!=NULL)
          (*k).num++;
    }



    
    for (i=0; i<num_of_keywords; i++)
        printf("%s %d\n",keywords[i].word, keywords[i].num);



}