Skip to content

Commit 6bb521d

Browse files
New Commit
1 parent eff90f3 commit 6bb521d

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

a.exe

41.3 KB
Binary file not shown.

remove_comment_whitespace.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@ void removeComments(char* prgm) {
1414
putchar(prgm[i]); // Print newline character
1515
line_number++;
1616
} else if (m_cmt && prgm[i] == '*' && prgm[i + 1] == '/') {
17-
m_cmt = false;
18-
i++;
17+
m_cmt = false, i++;
1918
} else if (s_cmt || m_cmt) {
2019
// Skip characters inside comments
2120
} else if (prgm[i] == '/' && prgm[i + 1] == '/') {
22-
s_cmt = true;
23-
i++;
21+
s_cmt = true, i++;
2422
} else if (prgm[i] == '/' && prgm[i + 1] == '*') {
25-
m_cmt = true;
26-
i++;
23+
m_cmt = true, i++;
2724
} else {
2825
putchar(prgm[i]);
2926
}
@@ -35,10 +32,9 @@ void removeComments(char* prgm) {
3532
void removeSpaces(char* s) {
3633
char* d = s; // Destination pointer
3734
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++;
4238
}
4339
*d = '\0'; // Null-terminate the modified string
4440
}
@@ -60,10 +56,15 @@ int main() {
6056
prgm[i] = '\0';
6157
fclose(file);
6258

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
6664
removeSpaces(prgm);
6765

66+
// Remove comments
67+
removeComments(prgm);
68+
6869
return 0;
6970
}

remove_comment_whitespace.exe

-86 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)