c語言實驗——合法的C識別符號
阿新 • • 發佈:2018-12-22
C語言實驗——合法的C識別符號
Time Limit: 1000 ms Memory Limit: 65536 KiBProblem Description
給出一個識別符號,請你判斷它是否是C語言合法的識別符號。Input
輸入一個識別符號,長度不超過100。Output
判斷是否合法,如果是輸出YES,否則輸出NO。Sample Input
123You
Sample Output
NO
Hint
C語言規定:識別符號只能由字母、數字和下劃線3種字元組成,且第一個字元必須為字母或下劃線。//package hello; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); String str = cin.next(); char ch; int len = str.length(); ch = str.charAt(0); if(ch >= '0' && ch <= '9') System.out.println("NO"); else { int i; for(i = 0; i < len; i++) { ch = str.charAt(i); if(i == 0) { if((ch>='a'&&ch<='z') || (ch>='A'&&ch<='Z') || ch=='_') continue; else break; } if(i != 0) { if((ch>='a'&&ch<='z') || (ch>='A'&&ch<='Z') || (ch>='0'&&ch<='9') || ch=='_') continue; else break; } } if(i >= len) { System.out.println("YES"); } else { System.out.println("NO"); } } } }