题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2024。

C语言标记符:1、首字母以__(下划线)开头或者字母开头,不能用数字。 2、中间的可以为数字,字母或者__(下划线)

小技巧:#include<ctype.h>的应用。。isalpha(sz1[i])若是字母则返回非值,否则返回零。isdigit(sz1[i])和isspace(sz1[i])依次类推。

CODE:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<math.h>
 5 #include<ctype.h>
 6 #include<algorithm>
 7 using namespace std;
 8 
 9 
10 const int maxn = 1000;
11 
12 char sz1[maxn];
13 
14 
15 int main()
16 {
17     int t;
18     scanf("%d" ,&t);
19     getchar();
20     while(t--)
21     {
22         int i;
23         gets(sz1);
24         int len = strlen(sz1);
25         for(i = 0 ; i < len ;i++)
26         {
27             if(i == 0)
28             {
29                 if(!isalpha(sz1[0]) && sz1[0]!='_')
30                 {
31                     break;
32                 }
33             }
34             if(!isdigit(sz1[i]) && !isalpha(sz1[i]) && sz1[i]!='_')
35             {
36                 break;
37             }
38         }
39         if(i == len)
40         {
41             printf("yes\n");
42         }
43         else
44         {
45             printf("no\n");
46         }
47     }
48     return 0;

49 } 

 

posted on 2012-05-22 21:35  有间博客  阅读(229)  评论(0)    收藏  举报