-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
82 lines (72 loc) · 1.66 KB
/
main.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *oath_parts[] = {
"On my honor",
"I will do my best",
"to do my duty",
"to God and my country,",
"to help other people at all times,",
"to obey the Scout Law,",
"and to keep myself",
"physically strong",
"mentally awake",
"and morally straight.",
NULL
};
const char * findPhrase(const char *clue);
void doStudyLoop();
int main(int argc, char *argv[]) {
if (argc==2 && 0==strcmp(argv[1],"-all")) {
int i;
for (i=0; oath_parts[i]; i++) {
printf("%s ",oath_parts[i]);
if (i%2==1) {
printf("\n");
}
}
} else if (argc==2) {
const char * phrase;
phrase = findPhrase(argv[1]);
if (phrase) {
printf("%s\n",phrase);
} else {
fprintf(stderr,"(not found)");
return 1;
}
} else {
doStudyLoop();
}
return 0;
}
const char * findPhrase(const char *clue) {
int i;
for (i=0; oath_parts[i]; i++) {
if (strstr(oath_parts[i], clue)
&& oath_parts[i+1]) {
return oath_parts[i+1];
}
}
return NULL;
}
void chomp(char * s);
void doStudyLoop() {
char line[81];
const char *nextPhrase;
while (NULL != fgets(line, 80, stdin)) {
chomp(line);
nextPhrase = findPhrase(line);
if (nextPhrase) {
printf(" -> %s\n",nextPhrase);
} else {
printf("Not found... try again...\n");
}
}
}
void chomp(char *s) {
for (;*s;s++) {
if (*s == '\n') {
*s = 0;
}
}
}