473,503 Members | 10,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

K&R2 - section 1.5.3 , exercise 1-10

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
7 2366
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1649
by: arnuld | last post by:
exercise: counts spaces, tabs and newlines in the input for that, Richard Heathfield has created a C programme at his answers- page: http://users.powernet.co.uk/eton/kandr2/krx108.html i...
2
2276
by: arnuld | last post by:
there is a solution on "clc-wiki" for exercise 1.17 of K&R2: http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_17 i see this uses pointers whereas K&R2 have not discussed pointers...
8
4741
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
4
1550
by: arnuld | last post by:
as i said, i have restarted the book because i overlooked some material. i want to have some comments/views on this solution. it runs fine, BTW. ------------------ PROGRAMME -------------- /*...
16
1779
by: arnuld | last post by:
i have created solution which compiles and runs without any error/ warning but it does not work. i am not able to understand why. i thought it is good to post my code here for correction before...
19
2373
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program...
5
2901
by: arnuld | last post by:
this is a programme that counts the "lengths" of each word and then prints that many of stars(*) on the output . it is a modified form of K&R2 exercise 1-13. the programme runs without any...
1
1293
by: arnuld | last post by:
it works fine. any advice for improvement: ---------- PROGRAMME -------------- /* K&R2: section 1.5.3, exercise 1-9 STATEMENT: write a programme to copy its input to its output replacing a...
88
3663
by: santosh | last post by:
Hello all, In K&R2 one exercise asks the reader to compute and print the limits for the basic integer types. This is trivial for unsigned types. But is it possible for signed types without...
82
2744
by: arnuld | last post by:
PURPOSE :: see statement in comments GOT: Segmentation Fault I guess the segfault is sourced in the compile-time warning but I am giving a char* to the function already.
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7361
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5602
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.