As a celebration of good old Notepad, we are going to treat carriage returns and line feeds as what they originally meant, rather than what they are (ab-)used for today.
Given a string consisting of printable ASCII plus line feeds (␊; LF; esc \n
; hex 0A; dec 10) and carriage returns (␍; CR; esc \r
; hex 0D; dec 13), cause Try It Online to show how the printable characters would be positioned if printed on a printer which takes those two control characters literally:
- upon a line feed, continue printing one line further down
- upon a carriage return continue printing from the left edge
- multiple consecutive carriage returns behave like a single carriage return
Due to modern devices having problems with overstriking, a run of one or more of carriage returns will, except for at the beginning of the input, never occur without at least one preceding and/or following line feed. However, two runs of carriage returns may be separated by a single line feed.
Any amount of additional trailing white space is acceptable, both on the right-hand side of any lines and below the entire text, as long as at least the amount of white space given in the input is preserved.
Examples (using \n
and \r
for line feed and carriage return)
Lorem ipsum dolor sit amet,
Lorem ipsum dolor sit amet,
consectetur adipiscing\nelit, sed
consectetur adipiscing
elit, sed
do eiusmod\r\ntempor incididunt\n\n ut labore
do eiusmod
tempor incididunt
ut labore
et dolore\n\rmagna \r\r\naliqua. Ut
(notice the trailing spaces)
et dolore
magna
aliqua. Ut
\nenim ad minim veniam,\n\r quis nostrud
enim ad minim veniam, quis nostrud
\rexercitation\r\n\rullamco laboris\n\r\nnisi ut aliquip ex\n\n\rea commodo consequat.\n\n
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
stty -onlcr;cat
. \$\endgroup\$