-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordx.c
54 lines (50 loc) · 1.27 KB
/
wordx.c
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
/*--------------------------------------------------------------*
* Find words of the form <word><arg> *
*--------------------------------------------------------------*/
#include "d2.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
Dict d = Dict_open(NULL);
const char *word;
int i, tlen, len, minlen, maxlen;
if (argc != 2 && argc != 3) {
fprintf(stderr, "Usage: %s tail [length]\n", argv[0]);
exit(1);
}
tlen = strlen(argv[1]);
for (i=0; i<tlen; i++) {
if (! isalpha(argv[1][i])) {
fprintf(stderr, "Non-alphabetic character in tail\n");
exit(1);
}
argv[1][i] = toupper(argv[1][i]);
}
if (argc == 3) {
minlen = maxlen = atoi(argv[2]);
if (minlen < tlen+1 || minlen > MAXLEN) {
fprintf(stderr, "Invalid length %d\n", minlen);
exit(1);
}
} else {
minlen = tlen+2;
maxlen = MAXLEN;
}
if (minlen < 2) minlen = 2;
for (len=minlen; len<=maxlen; len++) {
Dscan ds = Dscan_open(d, len);
while ((word = Dscan_read(ds)) != NULL) {
if (strcmp(word+len-tlen, argv[1])==0) {
if (Dict_isword(d, word, len-tlen)) {
printf("%s\n", word);
}
}
}
Dscan_close(ds);
}
Dict_close(d);
return 0;
}