summaryrefslogtreecommitdiffstats
path: root/1/23.c
blob: fdb4a5b1096c7bc2e9189c516bb085fa21ea90ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
}