-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_comment.c
More file actions
58 lines (53 loc) · 1.23 KB
/
Copy pathremove_comment.c
File metadata and controls
58 lines (53 loc) · 1.23 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <string.h>
#define MAXLINE 100
int get_line();
char line[MAXLINE];
// Remove Comments like the one below from a program
/*
I am
a super
long comment
that should be deleted
and never seen
when running this program
on itself
*/
int main(){
while(get_line() > 0){
if(line[0] != '\0')
printf("%s", line);
}
return 0;
}
int get_line(){
int c, i;
for (i=0; i < MAXLINE-1 && (c=getchar()) != EOF && c != '\n'; ++i){
if(c == '/'){
c = getchar();
if(c == '/'){
// remove everything from the single line comment
while((c = getchar()) != '\n')
memset(line, '\0', sizeof line);
return 1;
} else if(c == '*'){
// Remove every single character between the stars
while((c = getchar()) != '*')
memset(line, '\0', sizeof line);
return 1;
} else {
// keep '/' operator use intact
line[i-1] = '/';
line[i] = c;
}
} else {
line[i] = c;
}
}
if(c == '\n'){
line[i] = c;
++i;
}
line[i] = '\0';
return i;
}