A
arnuld
any suggestions for improvement:
-------------- PROGRAMME -------------
/* K&R2: section 1.5.3 exercise 1-10
STATEMENT:
write a programme to copy its input to output, replacing each
TAB by '\t', BACKSPACE by '\b' and each backslash by '\\'.
*/
#include<stdio.h>
#define IN 1
#define OUT 0
int main()
{
int c = 0;
int state = OUT;
while((c = getchar()) != EOF)
{
if(c == '\t')
{
putchar('\\');
putchar('t');
state = IN;
}
if(c == '\b')
{
putchar('\\');
putchar('b');
state = IN;
}
if(c == '\\')
{
putchar('\\');
putchar('\\');
state = IN;
}
if(state == OUT)
putchar(c);
state = OUT;
}
return 0;
}
-------------- OUTPUT ------------------
[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra -O ex_1-10.c
[arch@voodo kr2]$ ./a.out
like this
like this
and this
and\t\tthis
and\backslash
and\\backslash
and\backslash & TAB
and\\backslash & TAB
opk
\t\t\topk
[arch@voodo kr2]$
-------------- PROGRAMME -------------
/* K&R2: section 1.5.3 exercise 1-10
STATEMENT:
write a programme to copy its input to output, replacing each
TAB by '\t', BACKSPACE by '\b' and each backslash by '\\'.
*/
#include<stdio.h>
#define IN 1
#define OUT 0
int main()
{
int c = 0;
int state = OUT;
while((c = getchar()) != EOF)
{
if(c == '\t')
{
putchar('\\');
putchar('t');
state = IN;
}
if(c == '\b')
{
putchar('\\');
putchar('b');
state = IN;
}
if(c == '\\')
{
putchar('\\');
putchar('\\');
state = IN;
}
if(state == OUT)
putchar(c);
state = OUT;
}
return 0;
}
-------------- OUTPUT ------------------
[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra -O ex_1-10.c
[arch@voodo kr2]$ ./a.out
like this
like this
and this
and\t\tthis
and\backslash
and\\backslash
and\backslash & TAB
and\\backslash & TAB
opk
\t\t\topk
[arch@voodo kr2]$