File tree Expand file tree Collapse file tree 3 files changed +14
-13
lines changed Expand file tree Collapse file tree 3 files changed +14
-13
lines changed Original file line number Diff line number Diff line change @@ -14,16 +14,13 @@ void removeComments(char* prgm) {
14
14
putchar (prgm [i ]); // Print newline character
15
15
line_number ++ ;
16
16
} else if (m_cmt && prgm [i ] == '*' && prgm [i + 1 ] == '/' ) {
17
- m_cmt = false;
18
- i ++ ;
17
+ m_cmt = false, i ++ ;
19
18
} else if (s_cmt || m_cmt ) {
20
19
// Skip characters inside comments
21
20
} else if (prgm [i ] == '/' && prgm [i + 1 ] == '/' ) {
22
- s_cmt = true;
23
- i ++ ;
21
+ s_cmt = true, i ++ ;
24
22
} else if (prgm [i ] == '/' && prgm [i + 1 ] == '*' ) {
25
- m_cmt = true;
26
- i ++ ;
23
+ m_cmt = true, i ++ ;
27
24
} else {
28
25
putchar (prgm [i ]);
29
26
}
@@ -35,10 +32,9 @@ void removeComments(char* prgm) {
35
32
void removeSpaces (char * s ) {
36
33
char * d = s ; // Destination pointer
37
34
while (* s ) {
38
- while (* s == ' ' )
39
- s ++ ; // Skip spaces
40
- if (* s )
41
- * d ++ = * s ++ ; // Copy non-space characters
35
+ if (* s != ' ' || (* (s + 1 ) != ' ' && * (s + 1 ) != '\n' ))
36
+ * d ++ = * s ; // Copy non-space characters
37
+ s ++ ;
42
38
}
43
39
* d = '\0' ; // Null-terminate the modified string
44
40
}
@@ -60,10 +56,15 @@ int main() {
60
56
prgm [i ] = '\0' ;
61
57
fclose (file );
62
58
63
- printf ("Given Program:\n%s\n\n" , prgm );
64
- printf ("Modified Program (without comments and extra spaces):\n" );
65
- removeComments (prgm );
59
+ printf ("Original program:\n" );
60
+ printf ("%s\n" , prgm );
61
+
62
+ printf ("\nModified Program (without comments and extra spaces):\n" );
63
+ // Remove whitespaces
66
64
removeSpaces (prgm );
67
65
66
+ // Remove comments
67
+ removeComments (prgm );
68
+
68
69
return 0 ;
69
70
}
You can’t perform that action at this time.
0 commit comments