[HDU] [2024] Valid C Language Identifiers

Problem Description

Loading...

Problem Analysis

“Valid” here means

  1. All characters are uppercase/lowercase letters, digits, or underscores (_)
  2. The first character cannot be a digit

So the code is easy to write
Note that the input may contain spaces, so scanf cannot be used

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<stdio.h>
#include<string.h>
int main() {
	int T;
	scanf("%d",&T);
	getchar();
	while(T--) {
		char s[55];
		gets(s);
		if(!((s[0]>='a'&&s[0]<='z')||(s[0]>='A'&&s[0]<='Z')||(s[0]=='_'))) {
			printf("no\n");
			continue;
		}
		int i;
		for(i=0; i<strlen(s); i++) {
			if(!((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')||(s[i]=='_')||(s[i]>='0'&&s[i]<='9'))) {
				printf("no\n");
				break;
			}
		}
		if(i<strlen(s))
			continue;
		printf("yes\n");
	}
	return 0;
}

Problem link: HDU 2024: Valid C Identifiers

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...