Problem Description
Identifier is an important concept in the C programming language. Identifiers provide names for several language elements, such as functions, variables, labels, etc.
An identifier is a sequence of characters. A valid identifier can contain only upper and lower case alphabetic characters, underscore and digits, and must begin with an alphabetic character or an underscore. Given a list of chararcter sequences, write a program to check if they are valid identifiers.
Input
The first line of the input contains one integer, N, indicating the number of strings in the input. N lines follow, each of which contains at least one and no more than 100 characters. (only upper and lower case alphabetic characters, digits, underscore (" "), hyphen ("-"), period ("."), comma (","), colon (":"), semicolon (";"), exclamation mark ("!"), question mark ("?"), single and double quotation marks, parentheses, white space and square brackets may appear in the character sequences.)
Output
For each of the N lines, output "Yes" (without quote marks) if the character sequence contained in that line make a valid identifier; output "No" (without quote marks) otherwise.
Example Input
7 ValidIdentifier valid_identifier valid_identifier 0 invalid identifier 1234567 invalid identifier adefhklmruvwxyz12356790 -.,:;!?'"()[]ABCDGIJLMQRSTVWXYZ
Example Output
Yes Yes Yes No No No No
题意:看c语言中标识符命名是否合理,首字符只能为字母或者下划线,命名只能有数字,字母,下划线;
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <math.h>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
#define MAX 200010
using namespace std;
int t;
int main()
{
char a[200];
int t;
scanf("%d\n",&t);
while(t--)
{
gets(a);
if(!isalpha(a[0])&&a[0]!='_') //首字母只能为字母或者下划线;
{
printf("No\n");
continue;
}
int flag=1;
int l=strlen(a);
for(int i=1;i<l;i++)
{
if(a[i]!='_'&&!isalnum(a[i]) && !(a[i]>='0' && a[i]<='9'))
{
flag=0;
break;
}
else
{
flag=1;
}
}
if(flag==1)
{
printf("Yes\n");
}
else if(flag==0)
{
printf("No\n");
}
}
return 0;
}