An identifier is a sequence of characters. A valid identifier can contain only upper and lower case alphabetic characters, underscore and digits, and must begin with an alphabetic character or an underscore. Given a list of chararcter sequences, write a program to check if they are valid identifiers.
For each of the N lines, output "Yes" (without quote marks) if the character sequence contained in that line make a valid identifier; output "No" (without quote marks) otherwise.
7 ValidIdentifier valid_identifier valid_identifier 0 invalid identifier 1234567 invalid identifier adefhklmruvwxyz12356790 -.,:;!?'"()[]ABCDGIJLMQRSTVWXYZ
Yes Yes Yes No No No No
示例程序
判断是否有特殊符号
ACcode:
#include <map> #include <queue> #include <cmath> #include <cstdio> #include <cstring> #include <stdlib.h> #include <iostream> #include <algorithm> #define maxn 1000 using namespace std; char s[maxn]; int loop; void doit(){ gets(s); int len=strlen(s); if((s[0]>='A'&&s[0]<='Z') || (s[0]>='a'&&s[0]<='z')|| s[0]=='_');else{ printf("No\n");return; } for(int i=1;i<len;++i) if((s[i]>='A'&&s[i]<='Z') || (s[i]>='a'&&s[i]<='z')||(s[i]<='9'&&s[i]>='0')|| s[i]=='_'); else{ printf("No\n"); return ; } printf("Yes\n"); } int main(){ scanf("%d",&loop);getchar(); while(loop--)doit(); return 0; } /************************************** Problem id : SDUT OJ 2163 User name : acmer Result : Accepted Take Memory : 488K Take Time : 0MS Submit Time : 2016-04-04 20:28:49 **************************************/