C语言----完成对输入的字符串中C关键词的查找统计。

完成对输入的字符串中C关键词的查找统计。
程序运行示例如下:
本程序将为您统计C语言的关键字的个数,请输入,输入end结束输入:
if do while while do break goto helloworld end
您的输入中C语言关键字出现的次数统计如下:
break : 1
do : 2
goto : 1
if : 1
while : 2

输入格式:
“本程序将为您统计C语言的关键字的个数,请输入,输入end结束输入:\n”
(既可以以空格分隔单词,也可以以回车分隔单词)
输出格式:
“您的输入中C语言关键字出现的次数统计如下:\n”
“%-10s: %6d\n”

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#define MAX 32
struct Count
{
    
    
	char* name;
	int count;
};

int main()
{
    
    
	char s[10] = {
    
     0 };
	struct Count arr[MAX] = {
    
         
		                                 "auto", 0,
										 "break",0,
										 "case",0,
										 "char",0,
										 "const", 0,
										 "continue",0,
										 "default",0,
										 "do", 0,
										 "double",0,
										 "else",0,
										 "enum",0,
										 "float",0,
										 "for",0,
										 "goto",0,
										 "if",0,
										 "int",0,
										 "long",0,
										 "register",0, 
	                                     "while", 0,
		                                 "reuturn",0,
		                                 "short",0,
		                                 "signed",0,
		                                 "sizeof",0,
		                                 "static", 0,
		                                 "struct",0,
		                                 "switch",0,
		                                 "typedef",0,
		                                 "union",0,
		                                 "unsigned",0,
		                                 "void", 0 ,
		                                 "volatile",0,
		                                 "while", 0,
	};
	int i;
	printf("本程序将为您统计C语言的关键字的个数,请输入,输入end结束输入:\n");
	while (strcmp(s, "end") != 0)
	{
    
    
		scanf("%s", s);
		for (i = 0; i < MAX-1; i++)
		{
    
    
			if (strcmp(s, arr[i].name) == 0)   // strcmp--字符串比较函数,如果两个字符串相等,返回值为0
			{
    
    
				arr[i].count++;
			}
		}
	}
	printf("您的输入中C语言关键字出现的次数统计如下:\n");
	for (i = 0; i < MAX-1; i++)
	{
    
    
		if (arr[i].count != 0)
		{
    
    
			printf("%-10s: %6d\n", arr[i].name, arr[i].count);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_52698632/article/details/113748902