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]$ 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.
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/.
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
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/ ?
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
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/
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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.
...
|
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 --------------
/*...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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.
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |
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 ...
|
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...
| | |