summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-11-22 18:49:22 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-11-22 18:49:22 +0800
commit05f202deafbd1684d5314937fa82f9f26defae87 (patch)
tree2e91dd0f351506204b1d79c52b29548edb177cee
parentf0d0f5d70412834d3b620d13fd413a169b9f24e9 (diff)
downloadk&r-exercises-05f202deafbd1684d5314937fa82f9f26defae87.tar.gz
1.23
-rw-r--r--1/23.c41
1 files changed, 41 insertions, 0 deletions
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 <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