A
arnuld
this runs fine and does what i want
any advice on making it better.
------------ PROGRAMME---------------
/* K&R2 section 1.5.3, exercise 1-9
STATEMENT:
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.
*/
#include <stdio.h>
int main()
{
int c;
int escape_counter = 0;
while((c = getchar()) != EOF)
{
escape_counter = 0;
if(c == '\t')
{
putchar('\\');
putchar('t');
escape_counter = 1;
}
if( c == '\b')
{
printf("\\b");
escape_counter = 1;
}
if( c == '\\')
{
printf("\\\\");
escape_counter = 1;
}
if( escape_counter == 0)
putchar(c);
}
return 0;
}
---------- OUTPUT -------------
[arch@voodo kr2]$ gcc -std=c99 -pedantic -Wall -Wextra ex_1-10.c
[arch@voodo kr2]$ ./a.out
like this
like this
and ti this
and ti\tthis
WOW this
WOW\t\tthis
how about this baclslash \
how about this baclslash \\
good
good\t\t\t
[arch@voodo kr2]$
any advice on making it better.
------------ PROGRAMME---------------
/* K&R2 section 1.5.3, exercise 1-9
STATEMENT:
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.
*/
#include <stdio.h>
int main()
{
int c;
int escape_counter = 0;
while((c = getchar()) != EOF)
{
escape_counter = 0;
if(c == '\t')
{
putchar('\\');
putchar('t');
escape_counter = 1;
}
if( c == '\b')
{
printf("\\b");
escape_counter = 1;
}
if( c == '\\')
{
printf("\\\\");
escape_counter = 1;
}
if( escape_counter == 0)
putchar(c);
}
return 0;
}
---------- OUTPUT -------------
[arch@voodo kr2]$ gcc -std=c99 -pedantic -Wall -Wextra ex_1-10.c
[arch@voodo kr2]$ ./a.out
like this
like this
and ti this
and ti\tthis
WOW this
WOW\t\tthis
how about this baclslash \
how about this baclslash \\
good
good\t\t\t
[arch@voodo kr2]$