-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ctokens.c
42 lines (35 loc) · 1.1 KB
/
Ctokens.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main() {
const char* keywords[] = {
"auto", "break", "case", "char", "const", "continue", "default", "do",
"double", "else", "enum", "extern", "float", "for", "goto", "if",
"int", "long", "register", "return", "short", "signed", "sizeof",
"static", "struct", "switch", "typedef", "union", "unsigned", "void",
"volatile", "while"
};
int num_keywords = sizeof(keywords) / sizeof(keywords[0]);
char word[100];
printf("Enter your code (end with Ctrl+D):\n");
int ch, i = 0;
while ((ch = getchar()) != EOF) {
if (!isalpha(ch)) {
continue;
}
i = 0;
do {
word[i++] = ch;
ch = getchar();
} while (isalnum(ch) || ch == '_');
word[i] = '\0';
for (int j = 0; j < num_keywords; ++j) {
if (strcmp(word, keywords[j]) == 0) {
printf("%s\n", word);
break;
}
}
}
return 0;
}