file-handling · beginner · ~10 min
Stream-rewrite a buffer while changing its length safely.
int normalize_eol(const char *in, char *out, size_t cap);
Copy in to out, replacing every \r\n with a single \n and dropping any
lone \r. NUL-terminate. Return bytes written, or -1 on NULL/overflow.
in; skip \r when the next char is \n (or always, for lone \r).cap before each write.Windows files arrive with CRLF; normalising to LF avoids the stray '\r' bugs that plague cross-platform text handling.
#include <stddef.h>
int normalize_eol(const char *in, char *out, size_t cap) {
/* TODO */
(void)in; (void)out; (void)cap;
return -1;
}
Only handling CRLF and leaving lone CRs. Forgetting the NUL's byte in the cap check.
Already-LF text is unchanged. A lone CR is removed. Tiny output buffer.
O(n).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.