HDU_oj_2024 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
 
分析:
注意点:
用gets()函数接收字符串,由于gets()函数会接收到前面输入数字n时的回车符,
所以在get()前需要用getchar()函数吸收这个回车符
 
PS:gets()函数和scanf()函数有个区别,scanf()函数输入数据时不会接收缓存区的空格和TAB和回车等,但gets()函数会接收。
puts()函数和printf()函数也有类似问题,printf()函数输出数据后不会换行,而puts()函数会自动换到下一行。
 
 
 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     char *s,ss[50];
 8     int n;
 9     cin>>n;
10     getchar();
11     while(n--)
12     {
13         gets(ss);
14         s=ss;
15         if((*s>='a' && *s<='z')||(*s>='A' && *s<='z')||(*s=='_'))
16         {
17             s++;
18             while((*s>='a' && *s<='z')||(*s>='A' && *s<='Z')
19                         ||(*s>='0' && *s<='9')||(*s=='_'))
20                 s++;
21             if(*s=='')
22             cout<<"yes"<<endl;
23             else
24             cout<<"no"<<endl;
25         }
26         else
27         cout<<"no"<<endl;
28     }
29 }
原文地址:https://www.cnblogs.com/tenjl-exv/p/7993684.html