Exercise 1-8 of the C programming language

Discussion in 'C Programming' started by Seebs, Aug 13, 2011.

  1. Seebs

    Seebs Guest

    On 2011-08-13, Joseph Santoyo <> 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 /
    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.
     
    Seebs, Aug 13, 2011
    #1
    1. Advertising

  2. 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);
    }
     
    Joseph Santoyo, Aug 13, 2011
    #2
    1. Advertising

  3. On Aug 13, 2:10 am, 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);
    >
    >
    >
    >
    >
    >
    >
    > }


    by without an else if I just meant, if statements.
     
    Joseph Santoyo, Aug 13, 2011
    #3
  4. On Aug 13, 2:04 am, Seebs <> wrote:
    > On 2011-08-13, Joseph Santoyo <> 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 / ://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    > I am not speaking for my employer, although they do rent some of my opinions.


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

    Your explanations are very clear.
     
    Joseph Santoyo, Aug 13, 2011
    #4
  5. On Aug 13, 3:03 am, "io_x" <> wrote:
    > "Joseph Santoyo" <> ha scritto nel messaggionews:...
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > 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


    Is it just as valid to put the ++bl, ++t, ++nl below the ifs?
     
    Joseph Santoyo, Aug 13, 2011
    #5
  6. Joseph Santoyo <> writes:

    > On Aug 13, 2:10 am, 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);
    >> }

    >
    > 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.
     
    Ben Bacarisse, Aug 13, 2011
    #6
  7. Seebs

    Tim Rentsch Guest

    Joseph Santoyo <> 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;
    }
     
    Tim Rentsch, Sep 4, 2011
    #7
  8. 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
     
    Robert Spanjaard, Sep 4, 2011
    #8
  9. On Sun, 04 Sep 2011 13:04:47 -0700, Tim Rentsch
    <> wrote:

    > Joseph Santoyo <> 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.
     
    David Thompson, Sep 9, 2011
    #9
  10. Seebs

    Tim Rentsch Guest

    David Thompson <> writes:

    > On Sun, 04 Sep 2011 13:04:47 -0700, Tim Rentsch
    > <> wrote:
    >
    >> Joseph Santoyo <> 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] );
     
    Tim Rentsch, Jan 24, 2012
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J Peterman
    Replies:
    8
    Views:
    444
    Mark McIntyre
    Oct 14, 2003
  2. Casey Hawthorne
    Replies:
    4
    Views:
    1,026
    Jarek Zgoda
    Aug 4, 2006
  3. Simon Morgan
    Replies:
    18
    Views:
    1,293
    Tim Rentsch
    Jul 24, 2005
  4. Jason
    Replies:
    12
    Views:
    766
    Peter 'Shaggy' Haywood
    May 2, 2007
  5. Christos Kokaliaris
    Replies:
    45
    Views:
    409
    Jorgen Grahn
    Jun 16, 2014
Loading...

Share This Page