Repository navigation
Expand file tree
/
Copy pathkeytab.c
More file actions
86 lines (79 loc) · 1.52 KB
/
Copy pathkeytab.c
File metadata and controls
86 lines (79 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAXWORD 100
#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,
"default",0,
"for",0,
"struct",0,
"unsigned",0,
"void",0,
"volatile",0,
"while",0,
};
int getword(char *,int);
int binsearch(char *,struct key *,int);
main()
{
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;
}
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;
}
int getword(char *word,int lim)
{
char *w = word;
int c;
while(isspace (c = getchar()))
;
if(c!=EOF)
*w++ = c;
if(!isalpha(c))
{
*w = '\0';
return c;
}
for(;--lim>0;w++)
if(!isalnum(*w = getchar()))
{
//ungetc(w);
*w = '\0';
break;
}
*w = '\0';
return word[0];
}