Exercise 1-8 of the C programming language


S

Seebs

Is there any way to write this code without an else if?
Sure!

If so how..and
can you explain it line by line please :/ I'm really new to C or
programming.
Welcome!

/* count blanks, tabs and newlines in input */
main()

You should probably spell this
int
main(void)
in modern C.
if (c == ' ')
++bl;
else if (c == '\t')
++t;
else if (c == '\n')
nl;
(You missed the "++" on the last one; it should be "++nl".)

You can write this without else if as follows:
if (c == ' ')
++bl;
if (c == '\t')
++t;
if (c == '\n')
++nl;

It's not necessarily any better or more efficient, but it'll work.

The way I'd probably do it would be:
switch (c) {
case ' ': ++bl; break;
case '\t': ++t; break;
case '\n': ++nl; break;
default: break;
}

'switch' jumps to a case matching the controlling expression, then executes
code until it hits a 'break'. It's often relatively efficient for cases
where there are many options; compilers may generate smarter code than for
the corresponding sequence of else if ()s.
printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
.... and
return 0;
here.

-s
 
Ad

Advertisements

J

Joseph Santoyo

Is there any way to write this code without an else if? If so how..and
can you explain it line by line please :/ I'm really new to C or
programming.

#include <stdio.h>

/* count blanks, tabs and newlines in input */
main()
{
int c, bl, t, nl;

c = 0;
bl = 0;
t = 0;
nl = 0;
while ((c = getchar()) != EOF)
if (c == ' ')
++bl;
else if (c == '\t')
++t;
else if (c == '\n')
nl;
printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
}
 
J

Joseph Santoyo

Is there any way to write this code without an else if? If so how..and
can you explain it line by line please :/ I'm really new to C or
programming.

#include <stdio.h>

/* count blanks, tabs and newlines in input */
main()
{
        int c, bl, t, nl;

        c = 0;
        bl = 0;
        t = 0;
        nl = 0;
        while ((c = getchar()) != EOF)
                if (c == ' ')
                        ++bl;
                else if (c == '\t')
                        ++t;
                else if (c == '\n')
                        nl;
        printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);







}

by without an else if I just meant, if statements.
 
J

Joseph Santoyo

You should probably spell this
        int
        main(void)
in modern C.


(You missed the "++" on the last one; it should be "++nl".)

You can write this without else if as follows:


It's not necessarily any better or more efficient, but it'll work.

The way I'd probably do it would be:
                switch (c) {
                case ' ': ++bl; break;
                case '\t': ++t; break;
                case '\n': ++nl; break;
                default: break;
                }

'switch' jumps to a case matching the controlling expression, then executes
code until it hits a 'break'.  It's often relatively efficient for cases
where there are many options; compilers may generate smarter code than for
the corresponding sequence of else if ()s.


... and
        return 0;
here.


-s

Hey this is the second time you help me! Thanks a lot man :D!!

Your explanations are very clear.
 
J

Joseph Santoyo

"Joseph Santoyo" <[email protected]> ha scritto nel messaggio












below  it is the need to add "{}" because the many if

#include <stdio.h>

/* count blanks, tabs and newlines in input */
int  main(void)
{int c, bl, t, nl;

 bl=0; t=0; nl=0;
 while((c=getchar())!=EOF)
    {if(c== ' ' ) ++bl;
     if(c== '\t') ++t;
     if(c== '\n') ++nl;
    }
 printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);}

---------------------------
or
#include <stdio.h>

/* count blanks, tabs and newlines in input */
int  main(void)
{int c, bl, t, nl;

 bl=0; t=0; nl=0;
 while((c=getchar())!=EOF)
    {bl+=(c== ' ' );
     t +=(c== '\t');
     nl+=(c== '\n');
    }
 printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);

}

don't know if is garantee that if c==' ' it return 1 and not 90
it seems yes because i seen it here

Is it just as valid to put the ++bl, ++t, ++nl below the ifs?
 
B

Ben Bacarisse

Joseph Santoyo said:
by without an else if I just meant, if statements.

You can do it without any selection statements at all:

while ((c = getchar() != EOF) {
bl += (c == ' ');
t += (c == '\n');
nl += (c == '\n');
}

Not saying you *should*, but then I don't understand why you want to avoid
"else if" in the first place!
 
Ad

Advertisements

T

Tim Rentsch

Joseph Santoyo said:
Is there any way to write this code without an else if? If so how..and
can you explain it line by line please :/ I'm really new to C or
programming.

#include <stdio.h>

/* count blanks, tabs and newlines in input */
main()
{
int c, bl, t, nl;

c = 0;
bl = 0;
t = 0;
nl = 0;
while ((c = getchar()) != EOF)
if (c == ' ')
++bl;
else if (c == '\t')
++t;
else if (c == '\n')
nl;
printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
}

// Disclaimer: not compiled
#include <limits.h>
#include <stdio.h>

int
main( void ){
int c;
static int counts[ UCHAR_MAX ];

while( c = getchar(), c != EOF ) counts[c]++;

printf( "There are %d blanks, %d tabs and %d newlines",
counts[' '], counts['\t'], counts['\n'] );

return 0;
}
 
R

Robert Spanjaard

Is there any way to write this code without an else if? If so how..and
can you explain it line by line please :/ I'm really new to C or
programming.

#include <stdio.h>

/* count blanks, tabs and newlines in input */ main()
{
int c, bl, t, nl;

c = 0;
bl = 0;
t = 0;
nl = 0;
while ((c = getchar()) != EOF)
if (c == ' ')
++bl;
else if (c == '\t')
++t;
else if (c == '\n')
nl;
printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
}

Just one thing that hasn't been mentioned yet: there's no need to give c
an initial value of 0.
 
D

David Thompson

int c;
static int counts[ UCHAR_MAX ];
YM UCHAR_MAX+1 .
while( c = getchar(), c != EOF ) counts[c]++;

printf( "There are %d blanks, %d tabs and %d newlines",
counts[' '], counts['\t'], counts['\n'] );
Nit: int (same as the OP had) could overflow. But so could long,
on many modern systems. unsigned int would make the results
well-defined though still wrong, and probably at no additional cost
(almost certainly undetectable beside the I/O cost anyway).

For completeness, another answer to the OP's literal question is
int c; /*unsigned?*/ int bl=0,t=0,nl=0, *p;
while( c=getchar(), c!=EOF ){
p = c==' '? &bl: c=='\t'? &t: c=='\n'? &nl: NULL;
if( p != NULL ) ++ *p;
}
printf /*as before or %u if unsigned*/

But really this is just 'else if' by another name.
 
Ad

Advertisements

T

Tim Rentsch

David Thompson said:
int c;
static int counts[ UCHAR_MAX ];
YM UCHAR_MAX+1 .

Thank you, yes I did.
while( c = getchar(), c != EOF ) counts[c]++;

printf( "There are %d blanks, %d tabs and %d newlines",
counts[' '], counts['\t'], counts['\n'] );
Nit: int (same as the OP had) could overflow. But so could long,
on many modern systems. unsigned int would make the results
well-defined though still wrong, and probably at no additional cost
(almost certainly undetectable beside the I/O cost anyway).

For completeness, another answer to the OP's literal question is
int c; /*unsigned?*/ int bl=0,t=0,nl=0, *p;
while( c=getchar(), c!=EOF ){
p = c==' '? &bl: c=='\t'? &t: c=='\n'? &nl: NULL;
if( p != NULL ) ++ *p;
}
printf /*as before or %u if unsigned*/

But really this is just 'else if' by another name.

A similar approach, without any short circuiting:

int c;
unsigned long counts[4] = {0};
while( c=getchar(), c!=EOF ){
counts[ (c==' ')*1 + (c=='\t')*2 + (c=='\n')*3 ] += 1;
}
printf( "...appropriate format...", counts[1], counts[2], counts[3] );
 

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

Top