-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-22.c
50 lines (42 loc) · 1.13 KB
/
1-22.c
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
// 1.22 Fold
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
void splitline(char str[], uint8_t splitlen) {
bool ins = false;
uint32_t offset = 0;
for (size_t i = 0; i < strlen(str) - 1; i++) {
if (ins && str[i] == ' ') {
str[i] = '\n';
ins = false;
offset = i % splitlen;
}
if ((i - offset) % splitlen == 0 && str[i] != '\n') {
for (size_t j = i; (int) j >= 0; j--) {
if (str[j] == '\n') {
ins = true;
break;
}
if (str[j] == ' ') {
str[j] = '\n';
break;
}
}
}
}
}
int main() {
char str[] = "This is a very long line and it must be shrunk! Yes it very well must I say! Yess yes yess!\n";
puts(str);
splitline(str, 20);
puts(str);
char str2[] = "This is a very loooooooooooooooooooooooooooong line and it must be shrunk! Yes it very well must I say! Yess yes yess!\n";
puts(str2);
splitline(str2, 20);
puts(str2);
char str3[] = "This is a very loooooooooooooooooooooooooooong line with a newline!\nHow's about them muffins, eh? Whatcha gonna do now?\nHmmmmmm???\n";
puts(str3);
splitline(str3, 20);
puts(str3);
}