[2011山东ACM省赛] Identifiers(模拟)

简介:

Identifiers

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描写叙述

 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.

输入

 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.)

输出

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.

演示样例输入

7
ValidIdentifier
valid_identifier
valid_identifier
0 invalid identifier
1234567
invalid identifier
adefhklmruvwxyz12356790 -.,:;!?'"()[]ABCDGIJLMQRSTVWXYZ

演示样例输出

Yes
Yes
Yes
No
No
No
No

提示

 

来源

山东省第二届ACM大学生程序设计竞赛

 

解题思路:

推断是否输入的字符串为合法标识符。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
using namespace std;
string str[1000];
int main()
{
    int n;cin>>n;
    getchar();//必须得加这个,吸收空格,否则后面用getline会出错
    for(int i=1;i<=n;i++)
    {
        getline(cin,str[i]);
        bool ok=1;
        int len=str[i].length();
        if(!isalpha(str[i][0])&&str[i][0]!='_')//首字母不是字母也不是下划线
        {
            cout<<"No"<<endl;
            continue;
        }
        for(int j=0;j<len;j++)
        {
            if(!isdigit(str[i][j])&&!isalpha(str[i][j])&&str[i][j]!='_')//不符合要求
            {
                ok=0;
                break;
            }
        }
        if(ok)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}


 

版权声明:本文博客原创文章,博客,未经同意,不得转载。






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4686198.html,如需转载请自行联系原作者


相关文章
2022天梯赛三月冲刺——PAT (Advanced Level)1013 Battle Over Cities (并查集找连通块)
2022天梯赛三月冲刺——PAT (Advanced Level)1013 Battle Over Cities (并查集找连通块)
90 0
Contest1041 - 2018年软件学院程序设计大赛(高年级组)重现赛
Contest1041 - 2018年软件学院程序设计大赛(高年级组)重现赛
upc2021秋组队训练赛第一场 ICPC North Central NA Contest 2020
upc2021秋组队训练赛第一场 ICPC North Central NA Contest 2020
86 0
upc2021秋组队训练赛第一场 ICPC North Central NA Contest 2020
UPC组队赛第三场——G: The Famous ICPC Team Again (主席树)
UPC组队赛第三场——G: The Famous ICPC Team Again (主席树)
90 0
|
算法
每日一题冲刺大厂第十三天提高组 第46届ICPC 东亚区域赛 A题
大家好,我是泡泡,给大家带来每日一题的目的是为了更好的练习算法,我们的每日一题提高组是为了有余力的同学准备的,让大家练到各种各样的题目,一年以后,蜕变成为一个不一样的自己!
122 0
|
Cloud Native 数据管理 关系型数据库
祝贺!我的同事李飞飞当选ACM Fellow、IEEE Fellow
因在数据库查询处理和优化以及云数据库系统方面所做出的卓越贡献而入选
736 0
祝贺!我的同事李飞飞当选ACM Fellow、IEEE Fellow
|
人工智能 Java BI
HDU - 2018杭电ACM集训队单人排位赛 - 2 - Problem D. Team Name
HDU - 2018杭电ACM集训队单人排位赛 - 2 - Problem D. Team Name
126 0
|
人工智能 Java
HDU - 2018杭电ACM集训队单人排位赛 - 3 - Problem E. Sequence
HDU - 2018杭电ACM集训队单人排位赛 - 3 - Problem E. Sequence
144 0
|
人工智能 Java
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem C. Sequence
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem C. Sequence
108 0