• C语言合法标识符


    Problem Description
    输入一个字符串,判断其是否是C的合法标识符。
     
    Input
    输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。
     
    Output
    对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出"yes",否则,输出“no”。
     
    Sample Input
    3
    12ajf
    fi8x_a
    ff ai_2
     
    Sample Output
    no
    yes
    no
     
     
    code:

    #include<stdio.h>
    #include<string.h>
    int main()
    {
             int n;
             int i;
             while(scanf("%d%*c",&n)!=EOF)
             {
                     while(n--)            //for(i=0;i<n;i++)则Wrong Answer
                     {
                           char str[51];
                                                      //memset(str,'',sizeof(str));
                           gets(str); //scanf("%s",str) 则 Output Limit Exceeded
                           if(str[0]=='_'||(str[0]<='z'&&str[0]>='a')||(str[0]<='Z'&&str[0]>='A'))
                            {
                                   int len,flag=1;
                                   len=strlen(str);
                                   for(i=0;i<len;i++)
                                   {
                                           if(str[i]=='_'||(str[i]<='z'&&str[i]>='a')||(str[i]<='Z'&&str[i]>='A')||(str[i]<='9'&&str[i]>='0'))
                                                      continue;
                                           else
                                          {
                                                flag=0;
                                                break;
                                          }
                                  }
                            if(flag)
                                 printf("yes ");
                           else
                                 printf("no ");
                         }
                      else
                            printf("no ");
                 }
          }
        return 0;
    }

     
     
     
     
  • 相关阅读:
    《代码大全2》阅读笔记08Chapter 15 Using Conditionals
    《代码大全2》阅读笔记09Chapter 16 Controlling Loops
    《代码大全2》阅读笔记12 Chapter 19 General Control Issues
    《代码大全2》阅读笔记13 Chapter 22 Developer Testing
    [转帖]Dictionary, SortedDictionary, SortedList 横向评测
    《代码大全2》阅读笔记07Chapter 12 Fundamental Data Types
    《代码大全2》阅读笔记11 Chapter 24 Refactoring
    《代码大全2》阅读笔记14 Chapter 23 Debugging
    New Concept English 3 01 A Puma at large
    (ZT)委托和事件的区别
  • 原文地址:https://www.cnblogs.com/gongpulin/p/3894012.html
Copyright © 2020-2023  润新知