Interview question: Write a function to remove the comments in the C/C ++ program code.

Source: Internet
Author: User

Write a function to remove the comments in the C/C ++ program code and return the results.

The following code is compiled in vc6 and tested in the source code file. The comments are removed successfully.

# Include <string. h> <br/> # include <stdlib. h> <br/> # include <stdio. h> <br/> # include <fcntl. h> <br/> # include <Io. h> </P> <p>/** // ** <br/> * function: remove comments from C/C ++ program code <br/> * input: pointer to C/C ++ Code <br/> */</P> <p> void <br/> remove_comment (char * Buf, size_t size) <br/>... {<br/> char * P, * end, C; <br/> char * sq_start, * dq_start; <br/> char * lc_start, * bc_start; <br/> size_t Len; </P> <p> P = Buf; <br/> end = P + Size; </P> <p> sq_start = NULL; <br/> dq_start = NULL; </P> <p> lc_start = NULL; <br/> bc_start = NULL; </P> <p> while (P <End )... {</P> <p> C = * P; </P> <p> switch (c )... {</P> <p> case ''':/** // * single quotes */<br/> If (dq_start | lc_start | bc_start )... {<br/>/** // * ignore single quotes in strings and comments */<br/> P ++; <br/> continue; <br/>}</P> <p> If (sq_start = NULL )... {<br/> sq_start = P ++; </P> <p>} else... {<br/> Len = P ++-sq_start; </P> <p> If (LEN = 2 & * (sq_start + 1) = '/')... {<br/>/** // * ignore single quotes */<br/> continue; <br/>}</P> <p> sq_start = NULL; <br/>}</P> <p> break; </P> <p> case '"': /** // * double quotes */<br/> If (sq_start | lc_start | bc_start )... {<br/>/** // * ignore double quotation marks in characters or comments */<br/> P ++; <br/> continue; <br/>}</P> <p> If (dq_start = NULL )... {<br/> dq_start = P ++; </P> <p>} else... {<br/> If (* (p ++- 1) = '/')... {<br/>/** // * ignore double quotation marks in a string */<br/> continue; <br/>}</P> <p> dq_start = NULL; <br/>}</P> <p> break; </P> <p> case '/': /** // * Diagonal Bar */<br/> If (sq_start | dq_start | lc_start | bc_start )... {<br/>/** // * ignore the Diagonal Bar in a character, string, or comment */<br/> P ++; <br/> continue; <br/>}</P> <p> C = * (p + 1); </P> <p> If (C = '/')... {<br/> lc_start = P; <br/> P + = 2; </P> <p>} else if (C = '*')... {<br/> bc_start = P; <br/> P + = 2; </P> <p>} else... {<br/> P ++; <br/>}</P> <p> break; </P> <p> case '*': /** // * asterisk */<br/> If (sq_start | dq_start | lc_start | bc_start = NULL )... {<br/>/** // * ignore the asterisks in characters, strings, or line comments */<br/> P ++; <br/> continue; <br/>}</P> <p> If (* (p + 1 )! = '/')... {<Br/>/** // * ignore the asterisk in the block comment */<br/> P ++; <br/> continue; <br/>}</P> <p> P + = 2; </P> <p> memset (bc_start, '', p-bc_start ); </P> <p> bc_start = NULL; </P> <p> break; </P> <p> case '': /** // * line break */<br/> If (lc_start = NULL )... {<br/> P ++; <br/> continue; <br/>}</P> <p> C = * (p-1 ); </P> <p> memset (lc_start, '', <br/> (C = ''? (P ++-1): p ++)-lc_start); </P> <p> lc_start = NULL; </P> <p> break; </P> <p> default: <br/> P ++; <br/> break; <br/>}</P> <p> If (lc_start )... {<br/> memset (lc_start, '', p-lc_start ); <br/>}</P> <p> int <br/> main (INT argc, char * argv []) <br/>... {<br/> int FD, N; <br/> char Buf [102400]; </P> <p> FD = open ("C: /Documents and Settings/Administrator/desktop/remove_comment.c ", <br/> _ o_rdonly, 0); <br/> If (FD =-1 )... {<br/> return-1; <br/>}</P> <p> N = read (FD, Buf, sizeof (BUF )); <br/> If (n =-1 | n = 0 )... {<br/> close (FD); <br/> return-1; <br/>}</P> <p> // printf ("test "); </P> <p> remove_comment (BUF, n); </P> <p> * (BUF + n) = '/0 '; <br/> printf (BUF); <br/> close (FD); <br/> return 0; <br/>}</P> <p> this article from the csdn blog, reprinted please indicate the source: http://blog.csdn.net/ngwsx/archive/2008/04/04/2250186.aspx

 

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.