1134:合法C标识符查
时间限制: 1000 ms 内存限制: 65536 KB
提交数: 56753 通过数: 22035
【题目描述】
给定一个不包含空白符的字符串,请判断是否是C语言合法的标识符号(注:题目保证这些字符串一定不是C语言的保留字)。
C语言标识符要求:
1.非保留字;
2.只包含字母、数字及下划线(“_”)。
3.不以数字开头。
【输入】
一行,包含一个字符串,字符串中不包含任何空白字符,且长度不大于20。
【输出】
一行,如果它是C语言的合法标识符,则输出yes,否则输出no。
【输入样例】
RKPEGX9R;TWyYcp
【输出样例】
no
这题很简单只需要判断几次就可以了
- 判断是不是保留字,下表就是c++的保留字;
asm do if return try auto double inline short typedef bool dynamic_cast int signed typeid break else long static typename case enum mutable static union catch explicit namespace static_cast unsigned char export new struct using calss extern operator switch virtual const FALSE private template void const_cast float protected this votatile continue for public throw vchar_t default friend register TRUE while delete goto reinterpret_cast
- 判断是不是只包含字母、数字及下划线(“_”)
- 判断是不是以数字开头。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i,l,h;
char a[256];
bool f=true,b=true;
cin.getline(a,256);
l=strlen(a)-1;
if((a[0]>='0')&&(a[0]<='9'))
{
b=false;
}
for(i=0;i<=l;i++)
{
if((a[i]>='0'&&a[i]<='9')||(a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z')||(a[i]=='_'));
else f=false;
}
if(f==true&&b==true)cout<<"yes";
else cout<<"no";
return 0;
}
简单,过
点个赞,点个关注吧