Hangzhou Electric 2024 (C language legal identifier)

Source: Internet
Author: User

Hangzhou Electric 2024

problem DescriptionEnter a string to determine whether it is a valid identifier for C.

InputThe input data contains multiple test instances, and the first row of the data is an integer n, representing the number of test instances, followed by n rows of input data, each of which is a string of no more than 50 length.

OutputFor each set of input data, output one line. If the input data is a valid identifier for C, output "yes", otherwise, output "no".

Sample Input
312ajffi8x_aff  ai_2
Sample Output
Noyes

No


Ideas:The code implementation is actually very simple, just the problem card after the loss of the number after the line break processing above.
Import java.util.*;class main{public static void Main (String args[]) {Scanner sc=new Scanner (system.in); int N=sc.nextint (); Sc.nextline (); while (n-->0) {String str=sc.nextline (); if (Str.charat (0) >= ' a ' &&str.charat (0) <= ' Z ') | | (Str.charat (0) >= ' A ' &&str.charat (0) <= ' Z ') | | (Str.charat (0) = = ' _ ')) {int k=0;for (int i=1;i<str.length (); i++) {       if (Str.charat (i) >= ' a ' &&str.charat (i) <= ' z ') | | (Str.charat (i) >= ' A ' &&str.charat (i) <= ' Z ') | | (Str.charat (i) >= ' 0 ' &&str.charat (i) <= ' 9 ') | | (Str.charat (i) = = ' _ ')) k++;} if (k== (Str.length ()-1)) {System.out.println ("yes");   } Else{system.out.println ("No");}} Else{system.out.println ("No");}}}

Note:In order for the newline to be loaded after the input number, there is the wrong approach:
Scanner sc1=new Scanner (system.in); Scanner sc2=new Scanner (system.in), while (Sc1.hasnext ()) {int n=sc1.nextint (), while (n-->0) {String str= Sc2.nextline ();
                ........

Looks like the line break is absorbed, in fact, there is no keyboard input, the line break has already existed in the data stream, this method just let two strings to install, and loaded into that string is not necessarily, so cannot ac!!!


Hangzhou Electric 2024 (C language legal identifier)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.