-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalize_file.cpp
55 lines (49 loc) · 1.4 KB
/
normalize_file.cpp
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
// #include <iostream>
// #include <fstream>
// #include <sstream>
#include <ctype.h>
// using namespace std;
void normalize_file(char *buf) {
if (*buf == '')
return;
int cur = 0;
int pos = 0;
int spaces_started = -1;
while (pos == 0 || *(buf + pos - 1) != '\0') {
if (*(buf + pos) == '\n' || *(buf + pos) == '\0') {
if (*(buf + pos) == '\0') {
if (*(buf + pos - 1) != '\n') {
*(buf + cur++) = '\n';
*(buf + cur++) = '\0';
} else {
*(buf + cur++) = '\0';
}
break;
} else {
*(buf + cur++) = '\n';
}
spaces_started = -1;
} else if (isspace(*buf + pos)) {
if (spaces_started == -1) {
spaces_started = pos;
}
} else {
if (spaces_started != -1) {
for (int j = spaces_started; j <= pos; ++j) {
*(buf + cur++) = *(buf + j);
}
spaces_started = -1;
} else {
*(buf + cur++) = *(buf + pos);
}
}
++pos;
}
}
// int main(int argc, char *argv[]) {
// char str[] = "first line \nsecond line";
// normalize_file(str);
// for (int i = 0; str[i] != '\0';i++){
// printf ("%c", str[i]);
// }
// }