HDUOJ - Problem - 2024 C语言合法标识符

HDUOJ - Problem - 2024 C语言合法标识符

Problem Description
输入一个字符串,判断其是否是C的合法标识符。

Input
输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。

Output
对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出"yes",否则,输出“no”。

Sample Input
3
12ajf
fi8x_a
ff ai_2

Sample Output
no
yes
no

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char temp[51];
char tempchar;
char Dic[38][12] = {"char","int","float","double","void","long","short","enum","struct",
					"union","signed","unsigned","for","do","while","break","continue",
					"goto","if","else","switch","case","default","return","auto",
					"static","extern","register","const","sizeof","typedef","volatile","restrict",
					"inline","_Complex","_Bool","_Imaginary","_Generic"};
int case_num;

int main(int argc, char const *argv[])
{
	scanf("%d",&case_num);
	getchar();
	while(case_num-- >= 1)
	{
		int temp_len = 0;
		while(tempchar = getchar(),tempchar != '\n')
		{
			temp[temp_len] = tempchar;
			temp_len++;
		}
		temp[temp_len] = '\0';
		if (!(((temp[0]) >= 65 && (temp[0]) <= 90) || 
			((temp[0]) >= 97 && (temp[0]) <= 122) ||
			((temp[0]) >= 95 && (temp[0]) <= 95)))
		{
			printf("no\n");
			goto end;
		}
		for (int temp_i = 0; temp_i < temp_len; ++temp_i)
		{
			if (!(((temp[temp_i]) >= 65 && (temp[temp_i]) <= 90) || 
			((temp[temp_i]) >= 97 && (temp[temp_i]) <= 122) ||
			((temp[temp_i]) >= 95 && (temp[temp_i]) <= 95) || 
			((temp[temp_i]) >= 48 && (temp[temp_i]) <= 57)))
			{
				printf("no\n");
				goto end;
			}
		}
		for (int Dic_i = 0; Dic_i < 38; ++Dic_i)
		{
			if (strcmp(temp,&(Dic[Dic_i][0])) == 0)
			{
				printf("no\n");
				goto end;
			}
		}
		printf("yes\n");
		end:;
	}
	system("pause");
	return 0;
}
发布了19 篇原创文章 · 获赞 23 · 访问量 3909

猜你喜欢

转载自blog.csdn.net/weixin_43737206/article/details/88727421