-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_comments.c
94 lines (70 loc) · 1.83 KB
/
remove_comments.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
int avoid_count_single_line_comment(char *a,int start)
{
int i,ans=0;
// Checking until '\n' is encountered
for(i=start;;i++)
{
if(a[i]=='\n')
return ans;
else
ans++;
}
}
int avoid_count_multi_line_comment(char *a,int start)
{
int i,ans=0;
// Checking until '*/' is encountered
for(i=start;;i++)
{
if(a[i]=='*' && a[i+1]=='/')
return ans;
else
ans++;
}
}
char * remove_comments(char *code)
{
int len=strlen(code),index=0,i;
static char ans[100000];
// Traversing the string
for(i=0;i<len;i++)
{
// If single_line_comment is encountered, then we get the skip_value from the avoid_count_single_line_comment() function and 'i' is incremented by the correpsonding skip_value(Adjusted with some measures)
if(code[i]=='/' && code[i+1]=='/')
{
i+=avoid_count_single_line_comment(code,i+2)+1;
}
// If multi_line_comment is encountered, then we get the skip_value from the avoid_count_multi_line_comment() function and 'i' is incremented by the correpsonding skip_value(Adjusted with some measures)
else if(code[i]=='/' && code[i+1]=='*')
{
i+=avoid_count_multi_line_comment(code,i+2)+3;
}
// The Character is inserted to the 'ans' string
else
ans[index++]=code[i];
}
return ans;
}
int main()
{
char code[10000],c;
int index=0;
FILE *fp;
fp=fopen("testing.c","r");
while(1)
{
c=fgetc(fp);
if(c==EOF)
break;
code[index++]=c;
}
printf("Here goes the original Code!!\n");
puts(code);
char *s=remove_comments(code);
printf("Here goes the Final Code!!\n");
puts(s);
return 0;
}