给定关键字,从终端输入一行以单词组成的字符串,统计各关键字出现的次数。
下面的程序在Dev-C环境下运行通过:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char key[5][9]={
"else","float","goto","if","register",
};
int main()
{
int i,j;
char str[100];
int a[5]={0};
printf("******统计关键词个数******\n");
printf("请输入一行字符串:\n");
gets(str);
char delims[] = " ";
char *res = NULL;
res = strtok(str,delims);
while(res){
for(i=0;i<5;i++){
if(!strcmp(key[i],res)){
a[i]++;
continue;
}
}
res = strtok(NULL,delims);
}
for(i=0;i<5;i++)
{
printf("%s:%d\n",key[i],a[i]);
}
system("pause");
return 0;
}