Simvik

Strip the line ending

Remove trailing CR and LF characters from a received line. An easy embedded C problem (Arrays and strings), run and graded in the browser.

easy · Arrays and strings · strings · uart

Lines received over a UART end in \n, \r\n or sometimes nothing at all.

Remove every trailing \r and \n from s, in place. Characters elsewhere in the string are left alone.

Starter code

void strip_newline(char *s) {
  // "OK\r\n" becomes "OK".
}

Example tests

CR LF

char s[] = "OK\r\n";
strip_newline(s);
printf("[%s]\n", s);

Expected output:

[OK]

LF only

char s[] = "data\n";
strip_newline(s);
printf("[%s]\n", s);

Expected output:

[data]

Learn it first

  • Arrays and strings · Decay, bounds, safe copying, and parsing and formatting without the heap.

More Arrays and strings problems