B
broli
This appears on page number 20 of K & R 2 exercise number 1-10
Q. 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 the
tabs and backspaces visible in an unambiguous way.
Here's my attempt :
#include<stdio.h>
int main(void)
{
int c;
char *s;
while( (c=getchar())!=EOF)
{
if( c == '\b' || c == '\t' || c == '\\')
{
if(c =='\b')
{
s = "\\b";
printf("%s",s);
}
if(c == '\t')
{
s = "\\t";
printf("%s",s);
}
if(c == '\\')
{
s = "\\\\";
printf("%s",s);
}
}
else
printf("%c",c);
}
return 0;
}
The program compiles successfully but when I execute the program it
seems that it can't handle backspaces( '\b' character). The program
works fine when it encounters thet tabs and backslashes. Can some one
please tell me what is wrong here ?
Q. 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 the
tabs and backspaces visible in an unambiguous way.
Here's my attempt :
#include<stdio.h>
int main(void)
{
int c;
char *s;
while( (c=getchar())!=EOF)
{
if( c == '\b' || c == '\t' || c == '\\')
{
if(c =='\b')
{
s = "\\b";
printf("%s",s);
}
if(c == '\t')
{
s = "\\t";
printf("%s",s);
}
if(c == '\\')
{
s = "\\\\";
printf("%s",s);
}
}
else
printf("%c",c);
}
return 0;
}
The program compiles successfully but when I execute the program it
seems that it can't handle backspaces( '\b' character). The program
works fine when it encounters thet tabs and backslashes. Can some one
please tell me what is wrong here ?