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
1 #include <iostream>
2 #include <stdio.h>
3 using namespace std; 4
5 int main() 6 { 7 int i,j,n; 8 cin>>n; 9 getchar(); 10 for(i=1;i<=n;i++){ 11 char a[110]; 12 cin.getline(a,110,'\n'); 13 if(('a'<=a[0] && a[0]<='z') || ('A'<=a[0] && a[0]<='Z') || a[0]=='_'){ 14 for(j=0;a[j];j++) 15 if(!(('a'<=a[j] && a[j]<='z') ||
16 ('A'<=a[j] && a[j]<='Z') ||
17 ('0'<=a[j] && a[j]<='9') ||
18 a[j]=='_') ) 19 break; 20 if(a[j]) 21 cout<<"No"<<endl; 22 else
23 cout<<"Yes"<<endl; 24 } 25 else
26 cout<<"No"<<endl; 27 } 28 return 0; 29 } 30
31 /************************************** 32 Problem id : SDUT OJ 2163 33 User name : Miracle 34 Result : Accepted 35 Take Memory : 452K 36 Take Time : 0MS 37 Submit Time : 2014-04-20 09:18:49 38 **************************************/
Freecode : www.cnblogs.com/yym2013