CCF NOI1079. 合法 C 标识符 (C++)

版权声明:代码属于原创,转载请联系作者并注明出处。 https://blog.csdn.net/weixin_43379056/article/details/85003444

1079. 合法 C 标识符

题目描述

给定一个不包含空白符的字符串,请判断是否是C语言合法的标识符号(注:题目保证这些字符串一定不是C语言的保留字)。

C语言标识符要求:

  1. 非保留字;
  2. 只包含字母、数字及下划线(“_”)。
  3. 不以数字开头。

输入

一行,包含一个字符串,字符串中不包含任何空白字符,且长度不大于20。

输出

一行,如果它是C语言的合法标识符,则输出yes,否则输出no。

样例输入

RKPEGX9R;TWyYcp

样例输出

no

数据范围限制

C++代码

include <iostream>
#include <string>
#include <cassert>

using namespace std;

bool checkValidIdentifierOfC(string s)
{
    char c = s[0];  // first char

    if (c>='0' && c<='9')
    {
        return false;
    }

    for(int i=0; i<s.size(); i++)
    {
        c = s[i];
        if ((c>='0' && c<='9') || (c>='a' && c<='z') ||
            (c>='A' && c<='Z') || (c == '_'))
        {
            continue;
        }
        else
        {
            return false;
        }
    }

    return true;
}
    
int main()
{
    string strText;

    cin >> strText;

    assert(strText.size() <= 20);

    if (true == checkValidIdentifierOfC(strText))
    {
        cout << "yes" << endl;
    }
    else
    {
        cout << "no" << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43379056/article/details/85003444
今日推荐