K&R2 - section 1.5.3 , exercise 1-10

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

mark_bluemel

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

use a switch structure and be consistent - you don't need printf()
here, I'd use putc() and perhaps fputs(). Like this:-

#include <stdio.h>

int main(void) {

int c;

while ((c = getc(stdin)) != EOF) {
switch(c) {
case('\t'): fputs("\\t",stdout);
break;
case('\b'): fputs("\\b",stdout);
break;
case('\\'): fputs("\\\\",stdout);
break;
default: putc(c,stdout);
break;
}
}
}

If you don't know (or don't like) switch, then use something like
this:-
#include <stdio.h>

int main(void) {

int c;

while ((c = getc(stdin)) != EOF) {
if(c == '\t') {
fputs("\\t",stdout);
} else if( c == '\b') {
fputs("\\b",stdout);
} else if(c == '\\') {
fputs("\\\\",stdout);
} else {
putc(c,stdout);
}
}
}

I put in lots of braces because it's house style and more maintenance
friendly IMHO.
 
A

arnuld

use a switch structure and be consistent - you don't need printf()
here, I'd use putc() and perhaps fputs(). Like this:-

i can't. i am at Chapter 1 where K&R2 did not discuss /switch-case/
yet, not even /else/ and /else-if/.
 
G

Gregor H.

i can't. i am at Chapter 1 where K&R2 did not discuss /switch-case/
yet, not even /else/ and /else-if/.
Right.
Indeed!

Here's my own (old) solution:

#include <stdio.h>

int main(void)
{
int c, special_char;

while ((c = getchar()) != EOF)
{
special_char = 0;

if (c == '\t')
{
putchar('\\');
putchar('t');

special_char = 1;
}

if (c == '\b')
{
putchar('\\');
putchar('b');

special_char = 1;
}

if (c == '\\')
{
putchar('\\');
putchar('\\');

special_char = 1;
}

if (special_char == 0)
putchar(c);
}

return 0;
}


G. H.
 
A

arnuld

:)

#include <stdio.h>

int main(void)
{
int c, special_char;

while ((c = getchar()) != EOF)
{
special_char = 0;

if (c == '\t')
{
putchar('\\');
putchar('t');

special_char = 1;
}

if (c == '\b')
{
putchar('\\');
putchar('b');

special_char = 1;
}

if (c == '\\')
{
putchar('\\');
putchar('\\');

special_char = 1;
}

if (special_char == 0)
putchar(c);
}

return 0;

}

i wanted to know why it is not a good idea to use /printf/ ?
 
G

Gregor H.

i wanted to know why it is not a good idea to use /printf/ ?
You snipped the answer. General rule in programming:

"be consistent"

!

Moreover (but this is just my own opinion) "printf" is simply "overkill"
here, since all you want to do is just printing 1 (or 2) character(s).
(Hence no need for "formatting" of any kind.)


G. H.
 
A

arnuld

You snipped the answer. General rule in programming:

"be consistent"
ok

Moreover (but this is just my own opinion) "printf" is simply "overkill"
here, since all you want to do is just printing 1 (or 2) character(s).
(Hence no need for "formatting" of any kind.)

you mean /printf/ and /formatting/, in general, are actually
"overhead" as compared to /putchar/
 
G

Gregor H.

you mean /printf/ and /formatting/, in general, are actually "overhead"
as compared to /putchar/
Overhead and "too much". (Why use a sophisticated function if the same
effect can be achived with a much simpler one?) I guess, I'm just fond of
the KISS principle. :)


G. H.
 

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

Staff online

Members online

Forum statistics

Threads
473,991
Messages
2,570,217
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top