From 05f202deafbd1684d5314937fa82f9f26defae87 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Mon, 22 Nov 2021 18:49:22 +0800 Subject: 1.23 --- 1/23.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1/23.c (limited to '1') diff --git a/1/23.c b/1/23.c new file mode 100644 index 0000000..fdb4a5b --- /dev/null +++ b/1/23.c @@ -0,0 +1,41 @@ +#include + +#define NONE 0 +#define SINGLLC 1 +#define MULTILC 2 +#define MAXLEN 1000 + +/* removes comments from a c program */ +int main(int argc, char *argv[]) { + int prev, curr, i, comment; + char s[MAXLEN]; + + i = prev = 0; + comment = NONE; + + while (i < MAXLEN - 1 && (curr = getchar()) != EOF) { + if (!comment) { + if (curr == '/' && prev == '/') { + comment = SINGLLC; + i--; + } else if (curr == '*' && prev == '/') { + comment = MULTILC; + i--; + } else { + s[i++] = curr; + } + } else if ((comment == SINGLLC && curr == '\n') || + (comment == MULTILC && curr == '/' && prev == '*')) { + comment = NONE; + } + + prev = curr; + } + + s[i] = 0; + + printf("c code: \n"); + printf("%s\n", s); + + return 0; +} \ No newline at end of file -- cgit v1.2.3