327 views
1 votes
1 votes

Ques) Write a program to copy its input to its output, replacing each tab by \t , each backspace by \b , and each backslash by \\ . This makes tabs and backspaces visible in an unambiguous way.

Ans.)

while ((c = getchar()) != EOF) {
    // Detect/translate special characters.

    if (c == '\t') {
        putchar ('\\');
        putchar ('t');
        continue;              // Go get next character.
    }

    if (c == '\b') {
        putchar ('\\');
        putchar ('b');
        continue;              // Go get next character.
    }

    if (c == '\\') {
        putchar ('\\');
        putchar ('\\');
        continue;              // Go get next character.
    }

    // Non-special, just echo it.

    putchar (c);
}

i didnt get this code, someone pls explain it .. why are we outputting "\\" and then "t" or "b" separately?

is it because , suppose we have a blank now we have to replace it with '\b' and now each '\' is to be replaced with '\\' . am i geting it right or wrong?

and also i didnt get the third case when c='\\' .

Please log in or register to answer this question.

Related questions

1 votes
1 votes
1 answer
1
shaurya vardhan asked Oct 2, 2017
419 views
Verify that the expression getchar() !=EOF is 0 or 1 . here , we can take input of any character and print its Getchar value , is that what we have to do?since at the ma...