判断C语言变量名是否合法

C语言实现判断变量名是否合法:

 

代码:

 

#include #include #include int main(){ int n,i,flag,len,flag1; char *ss[]={"auto","break","case","char","const","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof", "static", "struct", "switch", "typedef", "union", "unsigned","void", "volatile", "while"}; char s[1000]; scanf("%d",&n); getchar(); while(n--){ flag=1; flag1=1; gets(s); //printf("%s/n",s); len=strlen(s); if(s[0]=='_'||(s[0]>='a'&&s[0]<='z')||(s[0]>='A'&&s[0]<='Z')){ for(i=1;i='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')||(s[i]>='0'&&s[i]<='9'))) { flag=0; break; } } } else{ flag=0;//最终标记; } if(flag==1){ for(i=0;i<32;i++){ if(strcmp(ss[i],s)==0){ flag1=0; break; } } if(flag1==1){ //puts(s); printf("yes/n"); } else{ printf("no/n"); } } else{ printf("no/n"); } } //system("pause"); }

 

你可能感兴趣的:(C/C++)