1. 程式人生 > >hdu_problem_2024_C語言合法識別符號

hdu_problem_2024_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
*
*
*Author
*lcy
*
*
*Source
*C語言程式設計練習(四)
*
*
*Recommend
*lcy
*
*/
#include<iostream> #include<string> using namespace std; bool is_legal(string s) { for (int i = 0; i < s.size(); i++) { if ('a' <= s.at(i) && s.at(i) <= 'z' || 'A' <= s.at(i) && s.at(i) <= 'Z' || s.at(i) == '_' || '0' <= s.at(i) && s.at(i) <=
'9') { continue; } else { return false; } } return true; } int main() { int n; string s; cin >> n; getchar(); for (int i = 0; i < n; i++) { getline(cin,s); if ('a' <= s.at(0) && s.at(0) <= 'z' || 'A' <= s.at(0) && s.at(0) <= 'Z' || s.at(0) == '_'
) { if (is_legal(s)) cout << "yes\n"; else cout << "no\n"; } else cout << "no\n"; } system("pause"); return 0; }