Exercise 1-8 of the C programming language

  • Follow


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);
}
0
Reply josephsantoyo (7) 8/13/2011 6:10:06 AM

On 2011-08-13, Joseph Santoyo <josephsantoyo@gmail.com> wrote:
> 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
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
0
Reply usenet-nospam (2205) 8/13/2011 6:04:58 AM


On Aug 13, 2:10=A0am, Joseph Santoyo <josephsant...@gmail.com> wrote:
> 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()
> {
> =A0 =A0 =A0 =A0 int c, bl, t, nl;
>
> =A0 =A0 =A0 =A0 c =3D 0;
> =A0 =A0 =A0 =A0 bl =3D 0;
> =A0 =A0 =A0 =A0 t =3D 0;
> =A0 =A0 =A0 =A0 nl =3D 0;
> =A0 =A0 =A0 =A0 while ((c =3D getchar()) !=3D EOF)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if (c =3D=3D ' ')
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ++bl;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 else if (c =3D=3D '\t')
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ++t;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 else if (c =3D=3D '\n')
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 nl;
> =A0 =A0 =A0 =A0 printf("There are %d blanks, %d tabs and %d newlines", bl=
, t, nl);
>
>
>
>
>
>
>
> }

by without an else if I just meant, if statements.
0
Reply josephsantoyo (7) 8/13/2011 6:23:27 AM

On Aug 13, 2:04=A0am, Seebs <usenet-nos...@seebs.net> wrote:
> On 2011-08-13, Joseph Santoyo <josephsant...@gmail.com> wrote:
>
> > 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
> =A0 =A0 =A0 =A0 int
> =A0 =A0 =A0 =A0 main(void)
> in modern C.
>
> > =A0 =A0 =A0 =A0 =A0 =A0if (c =3D=3D ' ')
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0++bl;
> > =A0 =A0 =A0 =A0 =A0 =A0else if (c =3D=3D '\t')
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0++t;
> > =A0 =A0 =A0 =A0 =A0 =A0else if (c =3D=3D '\n')
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0nl;
>
> (You missed the "++" on the last one; it should be "++nl".)
>
> You can write this without else if as follows:
>
> > =A0 =A0 =A0 =A0 =A0 =A0if (c =3D=3D ' ')
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0++bl;
> > =A0 =A0 =A0 =A0 =A0 =A0if (c =3D=3D '\t')
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0++t;
> > =A0 =A0 =A0 =A0 =A0 =A0if (c =3D=3D '\n')
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0++nl;
>
> It's not necessarily any better or more efficient, but it'll work.
>
> The way I'd probably do it would be:
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 switch (c) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 case ' ': ++bl; break;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 case '\t': ++t; break;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 case '\n': ++nl; break;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 default: break;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
>
> 'switch' jumps to a case matching the controlling expression, then execut=
es
> code until it hits a 'break'. =A0It's often relatively efficient for case=
s
> where there are many options; compilers may generate smarter code than fo=
r
> the corresponding sequence of else if ()s.
>
> > =A0 =A0printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl=
);
>
> ... and
> =A0 =A0 =A0 =A0 return 0;
> here.
>
> > }
>
> -s
> --
> Copyright 2011, all wrongs reversed. =A0Peter Seebach / usenet-nos...@see=
bs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny picturesht=
tp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
> I am not speaking for my employer, although they do rent some of my opini=
ons.

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

Your explanations are very clear.
0
Reply josephsantoyo (7) 8/13/2011 6:40:13 AM

"Joseph Santoyo" <josephsantoyo@gmail.com> ha scritto nel messaggio
news:4a72458c-aad4-4fa5-b3d5-9692eb2e470a@w18g2000yqc.googlegroups.com...
> 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);
> }

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



0
Reply io_x 8/13/2011 7:03:32 AM

On Aug 13, 3:03=A0am, "io_x" <a...@b.c.invalid> wrote:
> "Joseph Santoyo" <josephsant...@gmail.com> ha scritto nel messaggionews:4=
a72458c-aad4-4fa5-b3d5-9692eb2e470a@w18g2000yqc.googlegroups.com...
>
>
>
>
>
>
>
>
>
> > 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 =3D 0;
> > bl =3D 0;
> > t =3D 0;
> > nl =3D 0;
> > while ((c =3D getchar()) !=3D EOF)
> > if (c =3D=3D ' ')
> > ++bl;
> > else if (c =3D=3D '\t')
> > ++t;
> > else if (c =3D=3D '\n')
> > nl;
> > printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
> > }
>
> below =A0it is the need to add "{}" because the many if
>
> #include <stdio.h>
>
> /* count blanks, tabs and newlines in input */
> int =A0main(void)
> {int c, bl, t, nl;
>
> =A0bl=3D0; t=3D0; nl=3D0;
> =A0while((c=3Dgetchar())!=3DEOF)
> =A0 =A0 {if(c=3D=3D ' ' ) ++bl;
> =A0 =A0 =A0if(c=3D=3D '\t') ++t;
> =A0 =A0 =A0if(c=3D=3D '\n') ++nl;
> =A0 =A0 }
> =A0printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);}
>
> ---------------------------
> or
> #include <stdio.h>
>
> /* count blanks, tabs and newlines in input */
> int =A0main(void)
> {int c, bl, t, nl;
>
> =A0bl=3D0; t=3D0; nl=3D0;
> =A0while((c=3Dgetchar())!=3DEOF)
> =A0 =A0 {bl+=3D(c=3D=3D ' ' );
> =A0 =A0 =A0t +=3D(c=3D=3D '\t');
> =A0 =A0 =A0nl+=3D(c=3D=3D '\n');
> =A0 =A0 }
> =A0printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
>
> }
>
> don't know if is garantee that if c=3D=3D' ' 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?
0
Reply josephsantoyo (7) 8/13/2011 7:28:04 AM

Joseph Santoyo <josephsantoyo@gmail.com> writes:

> On Aug 13, 2:10 am, Joseph Santoyo <josephsant...@gmail.com> wrote:
>> 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.

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!

-- 
Ben.
0
Reply ben.usenet (6515) 8/13/2011 1:18:13 PM

Joseph Santoyo <josephsantoyo@gmail.com> writes:

> 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;
}
0
Reply txr1 (1213) 9/4/2011 8:04:47 PM

On Fri, 12 Aug 2011 23:10:06 -0700, Joseph Santoyo wrote:

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

-- 
Regards, Robert                                      http://www.arumes.com
0
Reply spamtrap319 (5) 9/4/2011 9:07:54 PM

On Sun, 04 Sep 2011 13:04:47 -0700, Tim Rentsch
<txr@alumni.caltech.edu> wrote:

> Joseph Santoyo <josephsantoyo@gmail.com> writes:
> 
> > 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 */
<snip>
>     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.

0
Reply dave.thompson2 (767) 9/9/2011 5:28:30 AM

David Thompson <dave.thompson2@verizon.net> writes:

> On Sun, 04 Sep 2011 13:04:47 -0700, Tim Rentsch
> <txr@alumni.caltech.edu> wrote:
>
>> Joseph Santoyo <josephsantoyo@gmail.com> writes:
>> 
>> > 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 */
> <snip>
>>     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] );

0
Reply txr2511 (42) 1/24/2012 8:07:15 PM

10 Replies
23 Views

(page loaded in 0.41 seconds)


Reply: