-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.23_remove_comments.c
More file actions
43 lines (35 loc) · 881 Bytes
/
1.23_remove_comments.c
File metadata and controls
43 lines (35 loc) · 881 Bytes
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
// Program to remove all comments in a C program
#include <stdio.h>
void remove_comments();
char buff[20];
int main()
{
remove_comments();
printf("I can't see comments: %s\n", buff);
}
// get a line, if you see // ignore everything after until the new line character
void remove_comments()
{
int c, len;
int flag;
len = 0;
while((c = getchar()) != EOF)
{
buff[len] = c;
len++;
}
buff[len] = '\0';
/*
* Tiny searching algorithm that checks if the ith index and the next are
* comment characters, if so delete everything from there till
* the new line character.
*/
for (int i = 0; buff[i] != '\n'; i++){
if (buff[i] == '/' && buff[i + 1]== '/'){
buff[i] = '\0';
}
else if (buff[i] == '/' && buff[i + 1] == '*'){
buff[i] = '\0';
}
}
}