diff options
| author | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-11-22 18:49:22 +0800 |
|---|---|---|
| committer | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-11-22 18:49:22 +0800 |
| commit | 05f202deafbd1684d5314937fa82f9f26defae87 (patch) | |
| tree | 2e91dd0f351506204b1d79c52b29548edb177cee /1 | |
| parent | f0d0f5d70412834d3b620d13fd413a169b9f24e9 (diff) | |
| download | k&r-exercises-05f202deafbd1684d5314937fa82f9f26defae87.tar.gz | |
1.23
Diffstat (limited to '1')
| -rw-r--r-- | 1/23.c | 41 |
1 files changed, 41 insertions, 0 deletions
@@ -0,0 +1,41 @@ +#include <stdio.h> + +#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 |
