Identifiers(第二届ACM大学生程序设计竞赛 )

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值