1134:合法C标识符查(C C++)

【题目描述】

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

C语言标识符要求:

1.非保留字;

2.只包含字母、数字及下划线(“_”)。

3.不以数字开头。

【输入】

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

【输出】

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

【输入样例】

RKPEGX9R;TWyYcp

【输出样例】

no

【代码】

#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
    
    
	char a[25];
	cin>>a;	//输入字符串 
	int len=strlen(a);	
	if(a[0]>='0'&&a[0]<='9')  //第一项为数字情况 直接no 
	{
    
    
		cout<<"no";
		return 0;
	}
	for(int i=0;i<len;i++)//遍历数组 
	{
    
    	
		if( (a[i]>='A'&&a[i]<='Z') || (a[i]>='a'&&a[i]<='z') || (a[i]>='0'&&a[i]<='9') || a[i]=='_');//合法则不改变 
		else  //但凡遇到一次不合法则输出 no结束程序 
		{
    
    
			cout<<"no"; 
			return 0;
		}
	}
	//运行到这边  意味着没有不合法字符 则输出yes 
	cout<<"yes"; 
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_50901683/article/details/109076655
今日推荐