zoukankan      html  css  js  c++  java
  • 2024

    判断c语言的字符串是否是合法的变量名

    令人遗憾的是 我的for循环竟然多打了一个分号

    调试好久才发现

    我竟然瞎想是不是gets函数是不是有什么特殊属性 唉 天真

    #include <stdio.h>
    
    int main()
    {
        int n,i,j;
        char a[51];
        while (scanf("%d%*c",&n))
        {
            for (i = 0;i < n;i++)
            {
                gets(a);
                if ((a[0] >= 'a' && a[0] <= 'z') || (a[0] >= 'A' && a[0] <= 'Z') || (a[0] == '_'))
                {
                    for (j = 1; a[j] != ''; j++)
                    {
                        if ((a[j] >= 'a' && a[j] <= 'z') || (a[j] >= 'A' && a[j] <= 'Z') || (a[j] == '_') || (a[j] >= '0' && a[j] <= '9'))
                        {
                            printf("yes
    ");
                            break;
                        }
                        else
                        {
                            printf("no
    ");
                            break;
                        }
                    }
    
                }
                else
                {
                    printf("no
    ");
                }
            }
        }
        return 0;
    }
    

      

    参考答案

    				
    #include <ctype.h>
    #include <stdio.h>
    
    int main(void)
    {
        int n, d, i;
        char sym[64];
    
        scanf("%d%*c", &n);
        while (n--)
        {
            gets(sym);
            if (sym[0] != '_' && !isalpha(sym[0]))
            {
                puts("no");
                continue;
            }
            for (d = i = 1 ; sym[i] ; i++)
            {
                if (!isalnum(sym[i]) && sym[i] != '_')
                {
                    d = 0;
                    break;
                }
            }
            puts(d ? "yes" : "no");
        }
    
        return 0;
    }
    

      

    ========================if i have some wrong, please give me a message, thx.========================
  • 相关阅读:
    set集合 浅层拷贝会和深层拷贝
    "is"与"=="
    元组和字典
    运算符和列表
    Python 基础语法
    supervisor 安装配置详解
    如何运行vue项目
    过目不忘JS正则表达式
    vue Bus总线
    Robot Framework 环境安装(一)
  • 原文地址:https://www.cnblogs.com/ailx10/p/5343081.html
Copyright © 2011-2022 走看看