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

K&R2 - section 1.5.3 , exercise 1-8

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

*/
#include <stdio.h>

int main()
{
int c;
int ns = 0; /* number of SPACES */
int nt = 0; /* number of TABS */
int nn = 0; /* number of NEWLINES */

while((c = getchar()) != EOF)
{
if(c == ' ')
++ns;

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

if(c == '\n')
++nn;
}

printf("SPACES: %d\nTABS: %d\nNEWLINES: %d\n", ns, nt, nn);

return 0;
}

---------- OUTPUT ----------
[arch@voodo kr2]$ gcc -std=c99 -pedantic -Wall -Wextra ex_1-8.c
[arch@voodo kr2]$ ./a.out
like
SPACES: 0
TABS: 0
NEWLINES: 1
[arch@voodo kr2]$ ./a.out
like this
and this this this
SPACES: 2
TABS: 2
NEWLINES: 2
[arch@voodo kr2]$

Mar 20 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Mar 20, 4:16 am, "arnuld" <geek.arn...@gmail.comwrote:
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

*/

#include <stdio.h>

int main()
{
int c;
int ns = 0; /* number of SPACES */
int nt = 0; /* number of TABS */
int nn = 0; /* number of NEWLINES */

while((c = getchar()) != EOF)
{
if(c == ' ')
++ns;

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

if(c == '\n')
++nn;
}

printf("SPACES: %d\nTABS: %d\nNEWLINES: %d\n", ns, nt, nn);

return 0;

}

---------- OUTPUT ----------
[arch@voodo kr2]$ gcc -std=c99 -pedantic -Wall -Wextra ex_1-8.c
[arch@voodo kr2]$ ./a.out
like
SPACES: 0
TABS: 0
NEWLINES: 1
[arch@voodo kr2]$ ./a.out
like this
and this this this
SPACES: 2
TABS: 2
NEWLINES: 2
[arch@voodo kr2]$
My only comment is:
"Nicely done."

Mar 20 '07 #2

P: n/a
On Mar 21, 12:06 am, "user923005" <dcor...@connx.comwrote:

My only comment is:
"Nicely done."
:-)

Mar 21 '07 #3

P: n/a
On Mar 20, 9:56 pm, "arnuld" <geek.arn...@gmail.comwrote:
On Mar 21, 12:06 am, "user923005" <dcor...@connx.comwrote:
My only comment is:
"Nicely done."

:-)
One possible minor improvement is to connect the if()s

while((c = getchar()) != EOF)
{
if(c == ' ')
++ns;
else if(c == '\t')
++nt;
else if(c == '\n')
++nn;
}

The reason it might be considered a minor improvement is that the 2nd
and 3rd tests won't be performed if the previous tests return true.
You could similarly use a switch:

while((c = getchar()) != EOF)
{
switch (c) {
case ' ':
++ns;
break;
case '\t':
++nt;
break;
case '\n':
++nn;
break;
default:
break;
}
}

Which may (or may not) optimize a tiny bit better.
But I don't really prefer either of these two forms over your
original.
The switch form has the benfit that it expands easily to other cases
(e.g. '\r' etc. for whitespace or whatever else you might be
interested in counting.

Another interesting possibility might be to use an array dimentioned
UCHAR_MAX+1.
Then, for all characters that are read, you could just do this:
charlist[c]++;
and when the file was empty, you would have an instant count of any
and all characters.
The nice thing about this one is that it is branchless.
Mar 21 '07 #4

P: n/a
Groovy hepcat arnuld was jivin' on 20 Mar 2007 04:16:52 -0700 in
comp.lang.c.
K&R2 - section 1.5.3 , exercise 1-8's a cool scene! Dig it!
>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.
Nice job! A couple of fairly minor niggles, though.
>------------------ PROGRAMME --------------
/* K&R2 section 1.5.3, exercise 1-8
write a programme to count blanks, tabs and newlines
*/

#include <stdio.h>

int main()
{
int c;
int ns = 0; /* number of SPACES */
int nt = 0; /* number of TABS */
int nn = 0; /* number of NEWLINES */
These variables could be better named. Since you're counting the
number of blanks, tabs and newlines, you might call these variables
blanks, tabs and newlines, respectively. It's a small point in a
trivial program such as this one; but in a larger program, you'll
thank yourself for using more descriptive variable names.
while((c = getchar()) != EOF)
{
if(c == ' ')
++ns;
Here you've mixed tabs and spaces in the source code. This is always
a bad idea. What looks right/good in your source code editor may not
look good in my newsreader. (In fact, it doesn't.) Please use only
spaces (prefered) or only tabs for indentation. Many text editors can
be set to insert spaces up to user-defined tab stops instead of a tab.
I guess that's it.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Mar 24 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.