By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,419 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,419 IT Pros & Developers. It's quick & easy.

K&R2 - section 1.5.3 , exercise 1-10

P: n/a
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]$

Mar 20 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 20 Mar, 11:55, "arnuld" <geek.arn...@gmail.comwrote:
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.

Mar 20 '07 #2

P: n/a
On Mar 20, 5:32 pm, mark_blue...@pobox.com wrote:

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/.
Mar 20 '07 #3

P: n/a
On 20 Mar 2007 05:49:29 -0700, "arnuld" <ge*********@gmail.comwrote:
>>
use a switch structure
i can't. i am at Chapter 1 where K&R2 did not discuss /switch-case/
yet, not even /else/ and /else-if/.
Right.
>>
and be consistent - you don't need printf()
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.

--

E-mail: info<at>simple-line<Punkt>de
Mar 20 '07 #4

P: n/a
On Mar 20, 10:26 pm, Gregor H. <nomail@invalidwrote:
arnuld wrote:
i can't. i am at Chapter 1 where K&R2 did not discuss /switch-case/
yet, not even /else/ and /else-if/.

Right.
:-)
#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/ ?

Mar 20 '07 #5

P: n/a
On 20 Mar 2007 11:21:06 -0700, "arnuld" <ge*********@gmail.comwrote:
>
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.

--

E-mail: info<at>simple-line<Punkt>de
Mar 20 '07 #6

P: n/a
On Mar 21, 12:05 am, Gregor H. <nomail@invalidwrote:
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/

Mar 21 '07 #7

P: n/a
On 20 Mar 2007 21:55:02 -0700, "arnuld" <ge*********@gmail.comwrote:
>>
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/
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.

--

E-mail: info<at>simple-line<Punkt>de
Mar 21 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.