summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-11-22 20:06:27 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-11-22 20:06:27 +0800
commitce26f83b5871a2459e3d607be007cdb6e86b84e7 (patch)
tree0c17faf1807c151d56c7dcc4c414eeda4e57d288
parent6bdc8e7a1102b0536d6bdd7ae4d83b1f33b5784c (diff)
downloadk&r-exercises-ce26f83b5871a2459e3d607be007cdb6e86b84e7.tar.gz
1.23
-rw-r--r--1/23.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/1/23.c b/1/23.c
index 2a563cd..a75b0de 100644
--- a/1/23.c
+++ b/1/23.c
@@ -3,22 +3,26 @@
#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;
+ int prev, curr, i, comment, inquote;
char s[MAXLEN];
- i = prev = 0;
+ inquote = i = prev = 0;
comment = NONE;
while (i < MAXLEN - 1 && (curr = getchar()) != EOF) {
if (!comment) {
- if (curr == '/' && prev == '/') {
+ if (curr == '"' && prev != '\\') {
+ s[i++] = curr;
+ inquote = !inquote;
+ } else if (!inquote && curr == '/' && prev == '/') {
comment = SINGLLC;
i--;
- } else if (curr == '*' && prev == '/') {
+ } else if (!inquote && curr == '*' && prev == '/') {
comment = MULTILC;
i--;
} else {