Implements a gadget to remove C-language annotations _c language

Source: Internet
Author: User

Remove the annotations in C code,
1. Single note//;
2. Multi-line annotation/**/;
3. A single-line comment ends with "\" and the next line is also commented;
4. The annotations in the string are not processed.
Said to be C language, but in fact all of the C family can, such as Java.


Gadgets: removing C language annotations

Copy Code code as follows:

#include <stdio.h>

int main (int argc, char* argv[]) {
enum {
Literal
Single
multiple,
String
mode = literal;
Char last = 0, current;

while (current = GetChar ())!= EOF) {
Switch (mode) {
Case Single: {
if (last!= ' \ \ r ' && (current = = ' \ n ' | | | |
Putchar (current);
current = 0;
mode = literal;
}
} break;
Case multiple: {
if (last = = ' * ' && current = = ') {
current = 0;
mode = literal;
}
} break;
Case string: {
if (last = = ' \ ') {
Putchar (last);
Putchar (current);
else if (current!= ' \ \) {
Putchar (current);
if (current = = "") {
mode = literal;
}
}
} break;
Default: {
if (last = = '/') {
if (current = = '/') {
mode = single;
else if (current = = ' * ') {
mode = multiple;
} else {
Putchar (last);
Putchar (current);
}
else if (current!= '/') {
Putchar (current);
if (current = = "") {
mode = string;
}
}
} break;
}
last = current;
}

return 0;
}

Test code

Copy Code code as follows:

#include <stdlib.h>
#include <stdio.h>

int main (int argc, char *argv[])
{
Not show\
Not show\
Not show
Not show
* Not show/*
int is; Not show
int/* not show */MS; * Not show/*
Double DS; Not show\
Not show\
Not show
Double DM; /* ...
Not show
Not show */float FS; /**
* Now Show
*/
float/**/FM;
Char cs[] = "AAA///***/";
Char cm1[] =/* Not show */"hello*/";
Char cm2[] = "/*redraiment" */Not show * * *;
/* printf ("/////"); */
return exit_success;
}

The code after processing

Copy Code code as follows:

#include <stdlib.h>
#include <stdio.h>

int main (int argc, char *argv[])
{

int is;
int MS;
Double DS;
Double DM; Float FS;
float FM;
Char cs[] = "AAA///***/";
Char cm1[] = "hello*/";
Char cm2[] = "/*redraiment";

return exit_success;
}

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.