473,587 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1838
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.co mwrote:
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*********@gm ail.comwrites:
On Apr 4, 1:48 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>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
2157
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 give me strange behavior. Here is the code I've written: /****************************************************************************** * K&R2...
2
2283
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...
8
4750
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
1557
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
1793
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...
19
2390
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
2912
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...
16
1721
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 that input array on terminal. (in "main") 3.) we will reverse the array. (calling "reverse" in "main") 4.) we will print that reversed array. (in...
88
3708
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
2780
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
7923
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...
0
7852
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...
0
8349
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5395
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...
0
3845
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...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
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 we have to send another system
0
1192
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...

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.