1164: 零起點學算法71——C語言合法標識符(存在問題)


1164: 零起點學算法71——C語言合法標識符

Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lld
Submitted: 1080  Accepted: 484
[Submit][Status][Web Board]

Description

輸入一個字符串,判斷其是否是C的合法標識符。 

 

Input

 

輸入數據包含多個測試實例,數據的第一行是一個整數n,表示測試實例的個數,然后是n行輸入數據,每行是一個長度不超過50的字符串。 

 

Output

 

對於每組輸入數據,輸出一行。如果輸入數據是C的合法標識符,則輸出"yes",否則,輸出“no”。 

 

Sample Input

 
3
12ajf
fi8x_a
ff  ai_2

 

Sample Output

no
yes
no

 

HINT

 

 請注意編譯器的差異,可以使用TEST功能獲得幫助。

 

Source

 
 1 #include<stdio.h>
 2 #include<ctype.h>
 3 int main(){
 4     int n;
 5     char a[50];
 6     scanf("%d%*c",&n);  
 7     while(n--){
 8             int d=1;
 9             gets(a);
10             if(a[0]!='_'&& !isalpha(a[0])){
11                 printf("no\n");
12                 }
13             else{
14                 for(int j=1;a[j]!='\0';j++){
15                     if(a[j]!='_'&&!isalnum(a[j])){
16                         
17                         printf("no\n");
18                         break;
19                     }
20                     else{
21                         printf("yes\n");
22                         break; 
23                     }
24                 }      
25             }        
26     }
27     return 0;
28 }

//按理來說,跳出第一層循環后不會輸出yes 為什么會有這種情況

AC代碼:

 1 #include<stdio.h>
 2 #include<ctype.h>
 3 int main(){
 4     int n;
 5     char a[50];
 6     while(scanf("%d%*c",&n)!=EOF){
 7     while(n--){
 8             int d=1;
 9             gets(a);
10             if(a[0]!='_'&& !isalpha(a[0])){
11                 printf("no\n");
12                 }
13             else{
14                 for(int j=1;a[j]!='\0';j++){
15                     if(a[j]!='_'&&!isalnum(a[j])){
16                         
17                         d=0;
18                         break;
19                     }
20                 }
21                 puts(d ? "yes" : "no");      
22             }        
23     }
24     }
25     return 0;
26 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM