输入一个字符串,判断其是否是C的合法标识符。
输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。
对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出"yes",否则,输出“no”。
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- while (sc.hasNext()) {
- int n = sc.nextInt();
- sc.nextLine();
- for (int i = 0; i < n; i++) {
- String s = sc.nextLine();
- char[] s1 = s.toCharArray();
- int j;
- for (j = 0; j < s1.length; j++) {
- if (!((s1[0] >= 65 && s1[0] <= 90) || (s1[0] == 95) || (s1[0] >= 97 && s1[0] <= 122))) {
- System.out.println("no");
- break;
- }
- if (!((s1[j] >= 65 && s1[j] <= 90) || (s1[j] == 95) || (s1[j] >= 97 && s1[j] <= 122) || (s1[j] >= 48 && s1[j] <= 57))) {
- System.out.println("no");
- break;
- }
- }
- if (j == s1.length) {
- System.out.println("yes");
- }
- }
- }
- }
- }