KR - visualize backspace

I come across this exercise KR 1-10:

Write a program to copy your input to your output, replace each tab with \ t, each backspace with \ b, and each backslash with \\.

Here is a very simple solution:

#include <stdio.h>

int main()
{
    int c;
    const char TAB = '\t';
    const char BACKSPACE = '\b';
    const char BACKSLASH = '\\';
    while( EOF != ( c = getchar() ) )
    {
        if( TAB == c )
        {
            printf( "\\t" );
        }
        else if( BACKSPACE == c )
        {
            printf( "\\b" );
        }
        else if( BACKSLASH == c )
        {
            printf( "\\\\" );
        }
        else
        {
            putchar( c );
        }
    }

    return 0;
}

I found that it works fine to render Tabboth \(backslash) but not Backspace. It looks like Backspaces is not remembered by the console? I'm on Ubuntu 14.04.


This seems like a similar problem, but not quite sure about it.

+4
source share
1 answer

, , . , , . getchar() backspace.

, , :

 $ printf 'foo\bbar\n' | ./a.out
 foo\bbar
+4

All Articles