Hangzhou Electric OJ 11 Ye 2024 // c judgment of legal identifier

Problem Description

An input string, it is determined whether the legal identifier C.

Input

Input data comprising a plurality of test example, the first data row n is an integer, indicates the number of test cases, then n lines of input data, each line a string length does not exceed 50.

Output

For each case, the output line. If the input data is valid C identifier, the output "yes", otherwise, output "no".

Sample Input

3
12ajf
fi8x_a
ff ai_2

Sample Output

no
yes
no

Code

#include"stdio.h"
#include"string.h" 
int main()
{
	int i,n,len,flag;
	char array[50];
	scanf("%d",&n);
	getchar();//获取回车符 
	while(n--)
	{
		flag = 0;//标记 
		gets(array);  // 输入字符串 
		len = strlen(array);//测量字符串长度 
		for (i = 0;i < len;i++)
		{
			if(array[i] == ' ')
				flag++;
		}//用于记录是否含有空格 
		if (flag != 0) 	printf("no\n");//如果flag不等于0就说明有空格,就是错误输入 
		else if('A' <= array[0] && array[0] <= 'Z'|| 'a' <= array[0] && array[0] <= 'z' || array[0] == '_')
		/*在判断首个字符是字母或者_的情况下进行下面的判断*/
		{
			for(i=1;i<len;i++)
				{
					if('A' <= array[i] && array[i] <= 'Z' || 'a' <= array[i] && array[i] <= 'z' || array[i] == '_' || 48 <= array[i] && array[i] <= 57)
						continue;  // 合法标识符即为第一位只能为字母或者下划线,剩下的只能为字母、数字下滑线 
					else break;
				}
			if(i == len)//如果说i已经等于了len,就意味着arry[1]--a[len]都是满足合法字符定义的 
				printf("yes\n");
			else
				printf("no\n");//否则就不满足 
		}
		else
			printf("no\n");//如果首字符不是字母,则直接输入no 
	}	
	return 0;
 }
Published 63 original articles · won praise 12 · views 4083

Guess you like

Origin blog.csdn.net/qq_45353823/article/details/100127577