473,794 Members | 2,752 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 2381
On 20 Mar, 11:55, "arnuld" <geek.arn...@gm ail.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",std out);
break;
case('\b'): fputs("\\b",std out);
break;
case('\\'): fputs("\\\\",st dout);
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",std out);
} else if( c == '\b') {
fputs("\\b",std out);
} else if(c == '\\') {
fputs("\\\\",st dout);
} 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...@po box.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*********@gm ail.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@invalid wrote:
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*********@gm ail.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@invalid wrote:
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*********@gm ail.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
1665
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 have also created my programme which runs fine. i want to have some comments on my programme, in comparison with Richard's.
2
2297
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 yet. i have created a solution myself by modifying the example programme of section 1.19. i tried to find the source-code of K&R2 using Google. i found the home
8
4764
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. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
4
1572
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 -------------- /* K&R2 section 1.5.3, exercise 1-8 write a programme to count blanks, tabs and newlines */
16
1815
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 looking at CLC-Wiki for K&R2 solutions: --------------- PROGRAMME ------------ /* K&R2 section 1.5.3, exercise 1-9 STATEMENT: write a programme to copy its input to output replacing
19
2408
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 to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical
5
2920
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 compile-error BUT it has a semantic BUG: what i WANT: I want it to produce a "horizontal histogram" which tells how many characters were in the 1st word, how many characters were in the second word by writing equal number of stars, *, at the...
1
1306
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 string of one more blanks with a single blank. */
88
3803
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 invoking undefined behaviour triggered by overflow? Remember that the constants in limits.h cannot be used.
82
2846
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
9671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.