Skip to content

Commit eff90f3

Browse files
New Commit
1 parent e4a623a commit eff90f3

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

insert_sort.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define MAX 100
33

44
int a[MAX];
5+
// Sorting method
56
void sort(int n){
67
int i, t, c;
78
for(i=1;i<n;i++){
@@ -15,6 +16,7 @@ void sort(int n){
1516
}
1617
}
1718

19+
//Main method
1820
int main(){
1921
int i, n;
2022
printf("Enter number of elements : ");

insert_sort.exe

0 Bytes
Binary file not shown.

remove_comment_whitespace.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <stdio.h>
2+
#include <stdbool.h>
3+
4+
// Function to remove comments from a C program
5+
void removeComments(char* prgm) {
6+
bool s_cmt = false; // Flag for single-line comment
7+
bool m_cmt = false; // Flag for multiline comment
8+
int i = 0; // Index for traversing the input program
9+
int line_number = 1; // Track line numbers
10+
11+
while (prgm[i] != '\0') {
12+
if (s_cmt && prgm[i] == '\n') {
13+
s_cmt = false;
14+
putchar(prgm[i]); // Print newline character
15+
line_number++;
16+
} else if (m_cmt && prgm[i] == '*' && prgm[i + 1] == '/') {
17+
m_cmt = false;
18+
i++;
19+
} else if (s_cmt || m_cmt) {
20+
// Skip characters inside comments
21+
} else if (prgm[i] == '/' && prgm[i + 1] == '/') {
22+
s_cmt = true;
23+
i++;
24+
} else if (prgm[i] == '/' && prgm[i + 1] == '*') {
25+
m_cmt = true;
26+
i++;
27+
} else {
28+
putchar(prgm[i]);
29+
}
30+
i++;
31+
}
32+
}
33+
34+
// Function to remove spaces from a string in-place
35+
void removeSpaces(char* s) {
36+
char* d = s; // Destination pointer
37+
while (*s) {
38+
while (*s == ' ')
39+
s++; // Skip spaces
40+
if (*s)
41+
*d++ = *s++; // Copy non-space characters
42+
}
43+
*d = '\0'; // Null-terminate the modified string
44+
}
45+
46+
int main() {
47+
// Read the input C file
48+
FILE* file = fopen("insert_sort.c", "r");
49+
if (!file) {
50+
printf("Error opening file.\n");
51+
return 1;
52+
}
53+
54+
char prgm[10000]; // Adjust the buffer size as needed
55+
int c;
56+
int i = 0;
57+
while ((c = fgetc(file)) != EOF) {
58+
prgm[i++] = c;
59+
}
60+
prgm[i] = '\0';
61+
fclose(file);
62+
63+
printf("Given Program:\n%s\n\n", prgm);
64+
printf("Modified Program (without comments and extra spaces):\n");
65+
removeComments(prgm);
66+
removeSpaces(prgm);
67+
68+
return 0;
69+
}

remove_comment_whitespace.exe

41.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)