473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

K&R2, 1.5.3, exercise 1-10

any suggestions for improvement:

-------------- PROGRAMME -------------
/* K&R2: section 1.5.3 exercise 1-10

STATEMENT:
write a programme to copy its input to output, replacing each
TAB by '\t', BACKSPACE by '\b' and each backslash by '\\'.

*/

#include<stdio.h>

#define IN 1
#define OUT 0

int main()
{
int c = 0;
int state = OUT;

while((c = getchar()) != EOF)
{
if(c == '\t')
{
putchar('\\');
putchar('t');
state = IN;
}

if(c == '\b')
{
putchar('\\');
putchar('b');
state = IN;
}

if(c == '\\')
{
putchar('\\');
putchar('\\');
state = IN;
}

if(state == OUT)
putchar(c);

state = OUT;
}

return 0;
}

-------------- OUTPUT ------------------
[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra -O ex_1-10.c
[arch@voodo kr2]$ ./a.out
like this
like this
and this
and\t\tthis
and\backslash
and\\backslash
and\backslash & TAB
and\\backslash & TAB
opk
\t\t\topk
[arch@voodo kr2]$

Apr 4 '07 #1
4 1821
arnuld wrote:
any suggestions for improvement:

-------------- PROGRAMME -------------
/* K&R2: section 1.5.3 exercise 1-10

STATEMENT:
write a programme to copy its input to output, replacing each
TAB by '\t', BACKSPACE by '\b' and each backslash by '\\'.

*/

#include<stdio.h>

#define IN 1
#define OUT 0

int main()
{
int c = 0;
int state = OUT;

while((c = getchar()) != EOF)
{
if(c == '\t')
{
putchar('\\');
putchar('t');
state = IN;
}

if(c == '\b')
{
putchar('\\');
putchar('b');
state = IN;
}

if(c == '\\')
{
putchar('\\');
putchar('\\');
state = IN;
}

if(state == OUT)
putchar(c);

state = OUT;
}

return 0;
}
A bit more concise:

#include<stdio.h>

int main()
{
int c = 0;

while((c = getchar()) != EOF)
{
switch( c )
{
case '\t':
putchar('\\');
putchar('t');
break;

case '\b':
putchar('\\');
putchar('b');
break;

case '\\':
putchar('\\');
putchar('\\');
break;

default:
putchar(c);
break;
}
}

return 0;
}

--
Ian Collins.
Apr 4 '07 #2
On Apr 4, 1:48 pm, Ian Collins <ian-n...@hotmail.comwrote:
arnuld wrote:
any suggestions for improvement:
-------------- PROGRAMME -------------
/* K&R2: section 1.5.3 exercise 1-10
STATEMENT:
write a programme to copy its input to output, replacing each
TAB by '\t', BACKSPACE by '\b' and each backslash by '\\'.
*/
#include<stdio.h>
#define IN 1
#define OUT 0
int main()
{
int c = 0;
int state = OUT;
while((c = getchar()) != EOF)
{
if(c == '\t')
{
putchar('\\');
putchar('t');
state = IN;
}
if(c == '\b')
{
putchar('\\');
putchar('b');
state = IN;
}
if(c == '\\')
{
putchar('\\');
putchar('\\');
state = IN;
}
if(state == OUT)
putchar(c);
state = OUT;
}
return 0;
}

A bit more concise:

#include<stdio.h>

int main()
{
int c = 0;

while((c = getchar()) != EOF)
{
switch( c )
{
case '\t':
putchar('\\');
putchar('t');
break;

case '\b':
putchar('\\');
putchar('b');
break;

case '\\':
putchar('\\');
putchar('\\');
break;

default:
putchar(c);
break;
}
}

return 0;

}

--
Ian Collins.
thanks Ian and i am still at chapter 1, where K&R2 have not started to
discuss "switch-case"

Apr 4 '07 #3
arnuld wrote:
>>
*Please* don't quote signatures!
>
thanks Ian and i am still at chapter 1, where K&R2 have not started to
discuss "switch-case"
Oops, I'd better keep my copy handy as you go!

--
Ian Collins.
Apr 4 '07 #4
"arnuld" <ge*********@gmail.comwrites:
On Apr 4, 1:48 pm, Ian Collins <ian-n...@hotmail.comwrote:
>arnuld wrote:
any suggestions for improvement:
-------------- PROGRAMME -------------
/* K&R2: section 1.5.3 exercise 1-10
STATEMENT:
write a programme to copy its input to output, replacing each
TAB by '\t', BACKSPACE by '\b' and each backslash by '\\'.
*/
<snip program with if (C1) S1; if (C2) S2; ... shape and state variable>
>>
A bit more concise:
<snip program with switch statement>
>>

thanks Ian and i am still at chapter 1, where K&R2 have not started to
discuss "switch-case"
Don't focus too much on syntax. Ian's program is clearer and more
concise even if you replace the switch with a nested if () {} else if
() {} ... else {} shape.

He is suggesting you don't need the extra (and confusing) state
variable and that you don't need to test if c == '\b' when you have
already determined that c == '\t' (i.e. think "else").

--
Ben.
Apr 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Chris Readle | last post by:
Ok, I've just recently finished a beginning C class and now I'm working through K&R2 (alongside the C99 standard) to *really* learn C. So anyway, I'm working on an exercise in chapter one which...
2
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
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
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
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
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
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...
16
by: arnuld | last post by:
i am not able to make it work. it compiles without any error but does not work: what i WANTED: 1.) 1st we will take the input in to an array. (calling "getline" in "main") 2.) we will print...
88
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...

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.