C-Language legal identifiers
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total Submission (s): 37056 Accepted Submission (s): 14897
Problem description Enter a string to determine whether it is a valid identifier for C.
Input data contains multiple test instances, the first row of the data is an integer n, which indicates the number of test instances, followed by n rows of input data, each of which is a string of not more than 50 in length.
Output outputs one row for each set of input data. If the input data is a valid identifier for C, output "yes", otherwise, output "no".
Sample Input
312ajffi8x_aff ai_2
Sample Output
Noyesno
Authorlcy
SOURCEC Language programming Exercises (iv)
Recommendlcy | We have carefully selected several similar problems for you:2025 2026 2021 2027 2030
--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------
This topic first need to know what is the C language legal identifier:
(1), the first element can only be underlined or letters,
(2), elements except the first element can only be numbers, underscores and letters)
First determine whether the first character conforms to the opening rule, and then each character is judged one after the other.
AC Code:
#include <iostream> #include <string>using namespace Std;int main () {string A;int n;cin >> N;getchar (); while (n--) {getline (cin, a); bool flag = TRUE;IF (!) ( A[0] = = ' _ ' | | (' A ' <= a[0] && a[0] <= ' z '))) {flag = False;cout << "no" << endl;continue;} for (int i = 0; i < a.size (); i++) {if (!) ( A[i] = = ' _ ' | | (A[i] >= ' 0 ' &&a[i] <= ' 9 ') | | (' A ' <= a[i] && a[i] <= ' Z ') | | (' A ' <= a[i] && a[i] <= ' z '))) {flag = False;cout << "no" << Endl;break;}} if (flag) cout << "yes" << Endl;} return 0;}
Hdoj 2024 C language legal identifier