473,657 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

K&R2, section 1.5.3, exercise 1.8

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.
---------------- MY PROGRAMME ---------------------
/* section
1.5.3
exercise
1.8

count the
blanks
*/
#include <stdio.h>

int main(void) {
int blanks = 0;
int tabs = 0;
int newlines = 0;
int c;

while((c=getcha r()) != EOF) {
if(c == ' ')
++blanks;

if( c == '\n')
++newlines;

if(c == '\t')
++tabs;

}

printf("%d \t %d\t %d\t \n", blanks, tabs, newlines);

return 0;
}

Mar 8 '07 #1
7 1653
arnuld said:
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
That version is no longer maintained. Please use the clc wiki version
instead:

http://clc-wiki.net/wiki/K%26R2_solu...1%3AExercise_8

Thanks.
>
i have also created my programme which runs fine. i want to have some
comments on my programme, in comparison with Richard's.
Your version looks fine to me.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 8 '07 #2
arnuld wrote:
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.
[ ... ]
#include <stdio.h>

int main(void) {
int blanks = 0;
int tabs = 0;
int newlines = 0;
int c;

while((c=getcha r()) != EOF) {
if(c == ' ')
++blanks;

if( c == '\n')
++newlines;

if(c == '\t')
++tabs;
Minor point but I'd write that as:

if(c == ' ') ++blanks;
else if(c == '\n') ++newlines;
else if(c == '\t') ++tabs;
else /* ... */

If any one test is true, the other tests are not attempted, while in
your version c will be compared against '\n' and '\t' even after it
evaluated equal to ' '. It's of course a very minor point. Also if the
number of comparisons get more than about five or six, (and they all
involve compile-time integral values), a switch construct may be a
cleaner way of expressing them.
}

printf("%d \t %d\t %d\t \n", blanks, tabs, newlines);
No need for the trailing tab and space.
>
return 0;
}
Mar 8 '07 #3
On Mar 8, 10:40 pm, "santosh" <santosh....@gm ail.comwrote:

Minor point but I'd write that as:

if(c == ' ') ++blanks;
else if(c == '\n') ++newlines;
else if(c == '\t') ++tabs;
else /* ... */
i have not reached a point where i can see "else" in K&R2. i am on
chapter 1.
If any one test is true, the other tests are not attempted, while in
your version c will be compared against '\n' and '\t' even after it
evaluated equal to ' '. It's of course a very minor point. Also if the
number of comparisons get more than about five or six, (and they all
involve compile-time integral values), a switch construct may be a
cleaner way of expressing them.
thanks for that.

printf("%d \t %d\t %d\t \n", blanks, tabs, newlines);

No need for the trailing tab and space.
"\t" produces a readable output and "space" increases the readability
here, IMVHO.

Mar 8 '07 #4
arnuld wrote:
On Mar 8, 10:40 pm, "santosh" <santosh....@gm ail.comwrote:
[ ... ]
printf("%d \t %d\t %d\t \n", blanks, tabs, newlines);
No need for the trailing tab and space.

"\t" produces a readable output and "space" increases the readability
here, IMVHO.
Indeed, but their effect is usually lost when placed at the end of a
line.

For example, here's a sample of what your program might output:

256 14 87

Removing the trailing tab and space will output:

256 14 87

See, big difference!

However, it's a minor issue and YMMV.

Mar 8 '07 #5
santosh wrote:
>
.... snip ...
>
Minor point but I'd write that as:

if(c == ' ') ++blanks;
else if(c == '\n') ++newlines;
else if(c == '\t') ++tabs;
else /* ... */
And, also as a minor style point, I would follow each if by at
least one blank. 'if' is neither a function nor a functional
macro, and the following blank tends to indicate that.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 9 '07 #6
"santosh" <sa*********@gm ail.comwrites:
arnuld wrote:
>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.
[ ... ]
>#include <stdio.h>

int main(void) {
int blanks = 0;
int tabs = 0;
int newlines = 0;
int c;

while((c=getcha r()) != EOF) {
if(c == ' ')
++blanks;

if( c == '\n')
++newlines;

if(c == '\t')
++tabs;

Minor point but I'd write that as:

if(c == ' ') ++blanks;
else if(c == '\n') ++newlines;
else if(c == '\t') ++tabs;
else /* ... */
Having comparisons and resulting executed statements on a single line is
disgusting style. At some point someone might be debugging that code and
it is a *pain in the ass* to step through such code. You cant tell if
the statements were executed half the time.
Mar 10 '07 #7
Richard wrote:
"santosh" <sa*********@gm ail.comwrites:
arnuld wrote:
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.
[ ... ]
#include <stdio.h>

int main(void) {
int blanks = 0;
int tabs = 0;
int newlines = 0;
int c;

while((c=getcha r()) != EOF) {
if(c == ' ')
++blanks;

if( c == '\n')
++newlines;

if(c == '\t')
++tabs;
Minor point but I'd write that as:

if(c == ' ') ++blanks;
else if(c == '\n') ++newlines;
else if(c == '\t') ++tabs;
else /* ... */

Having comparisons and resulting executed statements on a single line is
disgusting style.
Readability is often subjective. If there's only a single statement
for a control flow construct, I prefer to put it on the same line. I
don't think it's unreadable or disgusting.
At some point someone might be debugging that code and
it is a *pain in the ass* to step through such code. You cant tell if
the statements were executed half the time.
I've stepped through such code, both mine and those of others, and I
haven't encountered the difficulties you've mentioned.

Mar 10 '07 #8

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

Similar topics

2
2287
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
4752
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
1564
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
1799
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
7
2374
by: arnuld | last post by:
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 \\.
19
2395
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
2914
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...
4
1842
by: arnuld | last post by:
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 '\\'. */
88
3741
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.
6
2802
by: arnuld | last post by:
This is the example from section 4.2, page 71 of K&R2: double atof( char s ) { int i, sign; double val, power; for( i = 0; isspace( s ); ++i )
0
8420
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
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6176
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
4173
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...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
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
2
1733
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.