By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

K&R2, section 1.5.3, exercise 1.8

P: n/a
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;
}

Mar 8 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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=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.
>
return 0;
}
Mar 8 '07 #3

P: n/a
On Mar 8, 10:40 pm, "santosh" <santosh....@gmail.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

P: n/a
arnuld wrote:
On Mar 8, 10:40 pm, "santosh" <santosh....@gmail.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

P: n/a
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

P: n/a
"santosh" <sa*********@gmail.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=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.
Mar 10 '07 #7

P: n/a
Richard wrote:
"santosh" <sa*********@gmail.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=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.

Mar 10 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.