Blob


1 /*
2 * 1-23. Write a program to remove all comments from a C program. Don't forget to handle quoted strings and character constants properly. C comments do not nest.
3 */
5 #include <stdio.h>
6 #define WIDTH 80
7 #define TABSTOP 8
8 #define BEGINSLASH 0
9 #define COMMENT 1
10 #define ENDSTAR 2
11 #define NOTCOMMENT 3
12 #define QUOTE 4
14 int strln(char s[]);
15 void copy(char to[], char from[]);
17 main()
18 {
19 int c;
20 int comment = NOTCOMMENT;
21 while ((c = getchar())!=EOF) {
22 if (comment == BEGINSLASH) {
23 if (c == '*') {
24 comment = COMMENT;
25 } else {
26 printf("/");
27 if (c != '/') {
28 comment = NOTCOMMENT;
29 }
30 }
31 } else if (comment == COMMENT) {
32 if (c == '*') {
33 comment = ENDSTAR;
34 }
35 } else if (comment == ENDSTAR) {
36 if (c == '/') {
37 comment = NOTCOMMENT;
38 } else if (c != '*') {
39 comment = COMMENT;
40 }
41 } else if (comment == NOTCOMMENT) {
42 if (c == '/') {
43 comment = BEGINSLASH;
44 } else {
45 printf("%c", c);
46 }
47 }
48 }
49 }
50 int strln(char s[])
51 {
52 int i;
53 for (i=0; s[i] != '\0'; ++i)
54 ;
55 return i;
56 }
57 void copy(char to[], char from[])
58 {
59 int i;
60 i = 0;
61 while ((to[i] = from[i]) != '\0')
62 ++i;
63 }