Microsoft Internship Interview-Delete all comments for a Java file

Source: Internet
Author: User

Description: Delete all comments for a valid Java file, with a comment "//" or "/* * * * *" indicated.

Note: text in single or double quotation marks may include "//" or "/* * * * * *", but not part of the comment.

Solution: After reading a file, a character is processed in one character.

1  Public voidDeleteallcomment (InputStream is)throwsIOException {2         inti =Is.read ();3          while(I! =-1) {4             Charc = (Char) I;5             if(c = = '/')) {6                 Charj = (Char) Is.read ();7                 if(j = = '/')) {8 Deletethisline (IS);9}Else if(j = = ' * ') {Ten Deleteuntilend (IS); One                 } A}Else if(c = = ' \ ' | | c = = ' "')) { - echostring (is, c); -}Else { the System.out.print (c); -             } -i =Is.read (); -         } +     } -      +     //Delete to "//"Note at the beginning of the A     Private voidDeletethisline (InputStream is)throwsIOException { at         Chari = (Char) Is.read (); -         Charj = (Char) Is.read (); -          while(true) { -             if(i = = ' \ r ' && j== ' \ n ')) { -                  Break; -}Else { ini =J; -j = (Char) Is.read (); to             } +         } -     } the      *     //Delete comments that are denoted by "/* */" $     Private voidDeleteuntilend (InputStream is)throwsIOException {Panax Notoginseng         Chari = (Char) Is.read (); -         Charj = (Char) Is.read (); the          while(true) { +             if(i = = ' * ' && j== '/')) { A                  Break; the}Else { +i =J; -j = (Char) Is.read (); $             } $         } -     } -      the     //output the contents of single or double quotes as-is -     Private voidEchostring (InputStream is,intStartthrowsIOException {Wuyi         CharEnd = (Char) Is.read (); the          while(End! =start) { - System.out.print (end); WuEnd = (Char) Is.read (); -         } About}

Microsoft Internship Interview-Delete all comments for a Java file

Related Article

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.