K&R2, section 1.5.3, exercise 1.8


A

arnuld

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=getchar()) != 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;
}
 
Ad

Advertisements

R

Richard Heathfield

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&R2_solutions:Chapter_1:Exercise_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.
 
S

santosh

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

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=getchar()) != 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.
 
A

arnuld

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.

No need for the trailing tab and space.

"\t" produces a readable output and "space" increases the readability
here, IMVHO.
 
S

santosh

[ ... ]
"\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.
 
C

CBFalconer

santosh said:
.... 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.
 
Ad

Advertisements

R

Richard

santosh said:
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

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=getchar()) != 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.
 
Ad

Advertisements

S

santosh

Richard said:
santosh said:
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

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=getchar()) != 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.
 

Top