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()放错地方了。
 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

 

posted @ 2019-05-01 10:21  Auroras  阅读(958)  评论(0编辑  收藏  举报