Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
oorrja
GitHub Repository: oorrja/learntosolveit
Path: blob/master/languages/cprogs/Ex_1.23_remcomments.c
1240 views
1
/* Exercise 1.23
2
*
3
* Program to remove comments from a C Program.
4
*
5
* Program should echo quotes and character constants properly
6
* C comments do not nest
7
*
8
*/
9
10
#include<stdio.h>
11
12
void rcomment(int c);
13
void incomment(void);
14
void echo_quote(int c);
15
16
int main(void)
17
{
18
int c,d;
19
20
printf(" To Check /* Quoted String */ \n");
21
22
while((c=getchar())!=EOF)
23
rcomment(c);
24
25
return 0;
26
}
27
28
void rcomment(int c)
29
{
30
int d;
31
32
if( c == '/')
33
{
34
if((d=getchar())=='*')
35
incomment();
36
else if( d == '/')
37
{
38
putchar(c);
39
rcomment(d);
40
}
41
else
42
{
43
putchar(c);
44
putchar(d);
45
}
46
}
47
else if( c == '\''|| c == '"')
48
echo_quote(c);
49
else
50
putchar(c);
51
52
}
53
54
void incomment()
55
{
56
int c,d;
57
58
c = getchar();
59
d = getchar();
60
61
while(c!='*' || d !='/')
62
{
63
c =d;
64
d = getchar();
65
}
66
}
67
68
void echo_quote(int c)
69
{
70
int d;
71
72
putchar(c);
73
74
while((d=getchar())!=c)
75
{
76
putchar(d);
77
78
if(d == '\\')
79
putchar(getchar());
80
}
81
putchar(d);
82
}
83
84