K&R2 - section 1.5.3 , exercise 1-8

A

arnuld

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]$
 
U

user923005

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."
 
U

user923005


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.
 
P

Peter Shaggy Haywood

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"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top