Wednesday, January 4, 2012

[ANSI C] How to convert single CRs and LFs to CRLFs

Introduction

crlf2crlf function written in ANSI C converts all single CR (\r or \x13) and LF (\n or \x10) characters to CRLF (\r\n or \x13\x10) characters.

char *crlf2crlf(const char *text);

Returned string is allocated with malloc() function, and it is the caller's responsibility to free it with free() function.

Source code

https://github.com/vurdalakov/codeblog_examples/tree/master/ansi_c/crlf2crlf

Code is completely covered with test cases.

Usage

void printText(const char *text)
{
    char *convertedText;

    convertedText = crlf2crlf(text);

    printf(convertedText);

    free(convertedText);
}

No comments:

Post a Comment