c语言设计一个保留字(或关键字)的统计程序
时间: 2023-05-13 14:07:15 浏览: 100
可以使用C语言编写一个保留字统计程序,以下是示例代码:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORD_LEN 100
#define MAX_KEYWORDS 32
char *keywords[MAX_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 main()
{
char word[MAX_WORD_LEN];
int i, j, k, len, count;
char c;
count = 0;
while ((c = getchar()) != EOF) {
if (isalnum(c) || c == '_') {
i = 0;
word[i++] = c;
while ((c = getchar()) != EOF && (isalnum(c) || c == '_') && i < MAX_WORD_LEN) {
word[i++] = c;
}
word[i] = '\0';
len = strlen(word);
for (j = 0; j < MAX_KEYWORDS; j++) {
if (strcmp(word, keywords[j]) == 0) {
count++;
break;
}
}
}
}
printf("Total number of keywords: %d\n", count);
return 0;
}
```
这个程序可以统计输入中出现的C语言保留字的数量。它使用了一个字符串数组来存储所有的保留字,然后在输入中查找这些保留字。如果找到了一个保留字,就将计数器加一。
阅读全文