# Include <stdio. h>
# Include <string. h>
# Include <ctype. h>
# Define maxword 10
# Define nkeys (sizeof keytab/sizeof keytab [0])
Struct key
{
Char * word;
Int count;
} Keytab [] = {
{"Auto", 0 },
{"Break", 0 },
{"Case", 0 },
{"Char", 0 },
{"Const", 0 },
{"Continue", 0 },
{"Defalut", 0 },
{"Unsigned", 0 },
{"Void", 0 },
{"Volatile", 0 },
{"While", 0}
};
Int getword (char *, INT );
Int binsearch (char *, struct key *, INT );
Int getcounter (void );
Void ungetch1 (int c );
Int main (void)
{
Int N;
Char word [maxword];
While (getword (word, maxword )! = EOF)
If (isalpha (word [0])
If (n = binsearch (word, keytab, nkeys)> = 0)
Keytab [N]. Count ++;
For (n = 0; n <nkeys; n ++)
If (keytab [N]. Count> 0)
Printf ("% 4D % s \ n", keytab [N]. Count, keytab [N]. Word );
Return 0;
}
// Binsearch function: search for words from tab [0] To tab [n-1]
Int binsearch (char * word, struct key tab [], int N)
{
Int cond;
Int low, high, mid;
Low = 0;
High = n-1;
While (low <= high)
{
Mid = (low + high)/2;
If (cond = strcmp (word, Tab [Mid]. Word) <0)
High = mid-1;
Else if (cond> 0)
Low = Mid + 1;
Else
Return mid;
}
Return-1;
}
// Getword function: reads the next word or character from the input.
Int getword (char * word, int Lim)
{
Int C, getcounter (void );
Void ungetch1 (INT );
Char * w = word;
While (isspace (C = getspace ()))
;
If (C! = EOF)
* W ++ = C;
If (! Isalpha (c ))
{
* W = '\ 0 ';
Return C;
}
For (; -- Lim> 0; W ++)
If (! Isalnum (* w = getnum ()))
{
// Printf ("in the getword ()");
Ungetch1 (* w );
Break;
}
* W = '\ 0 ';
Return word [0];
}
# Define bufsize 100
Char Buf [bufsize];
Int bufp = 0;
Int getask( void)
{
Return (bufp> 0 )? Buf [-- bufp]: getchar ();
}
Void ungetch1 (int c)
{
If (bufp> = bufsize)
Printf ("ungetch: Too character characters \ n ");
Else
Buf [bufp ++] = C;
}