C语言合法标识符

Problem Description
输入一个字符串,判断其是否是C的合法标识符。
 

 

Input
输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。
 

 

Output
对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出"yes",否则,输出“no”。
 

 

Sample Input
3
12ajf
fi8x_a
ff ai_2
 

 

Sample Output
no
yes
no
 
        我去,这个题目一直wa,不知道卡在了哪里。看了别人博客才知道原来是getchar()放错地方了。
C语言合法标识符

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 using namespace std;
 4 int main()
 5 {
 6     //freopen("input.txt","r",stdin);
 7      int n;
 8      cin>>n;
 9      char s[51];
10      getchar();//一开始我放在了循环内
11     while(n--)
12    {
13 
14       gets(s);
15     int l=strlen(s);bool r=true;
16     for(int i=0;i<l;++i)
17     {
18         if(i==0) { if(!isalpha(s[0])&&s[0]!='_') {r=0;break;}}
19         else {if(!isalnum(s[i])&&s[i]!='_')  {r=false;break;}
20 
21                }
22 
23     }
24     if(r==0)  cout<<"no"<<endl;
25     else cout<<"yes"<<endl;
26 }
27   }

View Code

 

原文链接: https://www.cnblogs.com/Auroras/p/10799279.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C语言合法标识符

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/294465

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午3:59
下一篇 2023年2月15日 下午4:01

相关推荐