【杭电】[2024]C语言合法标识符

这里写图片描述
这里写图片描述

这里的合法是指
1.所有字符都是由大小写字母、数字、_下划线构成的
2.首字符不能为数字

所以可以轻松写出代码
需要注意的是因为输入有空格 所以不能用 scanf

#include<stdio.h>
#include<string.h>
int main() {
    int T;
    scanf("%d",&T);
    getchar();
    while(T--) {
        char s[55];
        gets(s);
        if(!((s[0]>='a'&&s[0]<='z')||(s[0]>='A'&&s[0]<='Z')||(s[0]=='_'))) {
            printf("no
");
            continue;
        }
        int i;
        for(i=0; i<strlen(s); i++) {
            if(!((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')||(s[i]=='_')||(s[i]>='0'&&s[i]<='9'))) {
                printf("no
");
                break;
            }
        }
        if(i<strlen(s))
            continue;
        printf("yes
");
    }
    return 0;
}

题目地址:【杭电】[2024]C语言合法标识符

原文地址:https://www.cnblogs.com/BoilTask/p/12569819.html