From 1c352d8c1dc97c8f6f11b7fbf38c621f53b73620 Mon Sep 17 00:00:00 2001 From: Vincent Douillet Date: Sun, 12 Dec 2021 08:42:25 +0100 Subject: day 8 --- input.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'input.c') diff --git a/input.c b/input.c index 1c72194..02461a1 100644 --- a/input.c +++ b/input.c @@ -40,6 +40,20 @@ void input_int_read(struct input_int* result, char* filename) { fclose(file); } +int str_replace(char* string, char a, char b) { + size_t i = 0; + int replace_count = 0; + while(string[i] != '\0') { + if(string[i] == a) { + string[i] = b; + replace_count++; + } + i++; + } + + return replace_count; +} + void input_str_read(struct input_str* result, char* filename) { // open input file FILE* file=fopen(filename, "r"); @@ -57,6 +71,9 @@ void input_str_read(struct input_str* result, char* filename) { char** dst = &(result->lines[lineIndex]); if(getline(dst, &lineSize, file) < 0) err(1, "read error line %ld\n", lineIndex); + + // cleanup line end char + str_replace(result->lines[lineIndex], '\n', '\0'); } fclose(file); -- cgit v1.2.3