K&R2, 1.5.3, exercise 1-10

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]$
 
I

Ian Collins

arnuld said:
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;
}
A bit more concise:

#include<stdio.h>

int main()
{
int c = 0;

while((c = getchar()) != EOF)
{
switch( c )
{
case '\t':
putchar('\\');
putchar('t');
break;

case '\b':
putchar('\\');
putchar('b');
break;

case '\\':
putchar('\\');
putchar('\\');
break;

default:
putchar(c);
break;
}
}

return 0;
}
 
A

arnuld

A bit more concise:

#include<stdio.h>

int main()
{
int c = 0;

while((c = getchar()) != EOF)
{
switch( c )
{
case '\t':
putchar('\\');
putchar('t');
break;

case '\b':
putchar('\\');
putchar('b');
break;

case '\\':
putchar('\\');
putchar('\\');
break;

default:
putchar(c);
break;
}
}

return 0;

}

thanks Ian and i am still at chapter 1, where K&R2 have not started to
discuss "switch-case"
 
I

Ian Collins

*Please* don't quote signatures!
thanks Ian and i am still at chapter 1, where K&R2 have not started to
discuss "switch-case"
Oops, I'd better keep my copy handy as you go!
 
B

Ben Bacarisse

arnuld said:
thanks Ian and i am still at chapter 1, where K&R2 have not started to
discuss "switch-case"

Don't focus too much on syntax. Ian's program is clearer and more
concise even if you replace the switch with a nested if () {} else if
() {} ... else {} shape.

He is suggesting you don't need the extra (and confusing) state
variable and that you don't need to test if c == '\b' when you have
already determined that c == '\t' (i.e. think "else").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top