Counting blanks

Discussion in 'C Programming' started by rajash@thisisnotmyrealemail.com, Nov 30, 2007.

  1. Guest

    Hi I am new to this forum.

    I have taken a class in C some time ago but now I am reading Kernigan
    and Richie's book to refresh my knowledge. I think I have forgotten
    alot and there are no solutions to the exercises. Maybe some people
    here can check through some of my solutions.

    Below is a solution to the exercise 1.8.

    /* blank counter */

    int x, y, z;

    void main()
    {
    char c;

    fflush(stdin); // start reading from stdin
    while(! feof(stdin) ) {
    switch(c = getchar()) {
    case ' ' : x++; break;
    case '\t': y++; break;
    case '\n': z++;
    }
    }
    fclose(stdin); // clean up

    printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);
    }
     
    , Nov 30, 2007
    #1
    1. Advertising

  2. Tor Rustad Guest

    wrote:
    > Hi I am new to this forum.


    A number of the basic errors in your code, is explained in the C FAQ.

    > I have taken a class in C some time ago but now I am reading Kernigan
    > and Richie's book to refresh my knowledge. I think I have forgotten
    > alot and there are no solutions to the exercises. Maybe some people
    > here can check through some of my solutions.
    >
    > Below is a solution to the exercise 1.8.
    >
    > /* blank counter */
    >
    > int x, y, z;
    >
    > void main()


    ???

    > {
    > char c;


    ???

    > fflush(stdin); // start reading from stdin


    ???

    > while(! feof(stdin) ) {


    No read errors possible?

    > switch(c = getchar()) {


    ???

    > case ' ' : x++; break;
    > case '\t': y++; break;
    > case '\n': z++;
    > }
    > }
    > fclose(stdin); // clean up


    ???

    >
    > printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);
    > }


    Try again, your code doesn't even compile:

    $ gcc -ansi -pedantic -W -Wall test.c
    test.c:4: warning: return type of âmainâ is not âintâ
    test.c: In function âmainâ:
    test.c:7: warning: implicit declaration of function âfflushâ
    test.c:7: error: âstdinâ undeclared (first use in this function)
    test.c:7: error: (Each undeclared identifier is reported only once
    test.c:7: error: for each function it appears in.)
    test.c:7: error: expected expression before â/â token
    test.c:15: error: expected expression before â/â token
    test.c:5: warning: unused variable âcâ
    $ cat -n test.c
    1 int x, y, z;
    2
    3 void main()
    4 {
    5 char c;
    6
    7 fflush(stdin); // start reading from stdin
    8 while(! feof(stdin) ) {
    9 switch(c = getchar()) {
    10 case ' ' : x++; break;
    11 case '\t': y++; break;
    12 case '\n': z++;
    13 }
    14 }
    15 fclose(stdin); // clean up
    16
    17 printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);
    18 }

    --
    Tor < | tr i-za-h a-z>
     
    Tor Rustad, Nov 30, 2007
    #2
    1. Advertising

  3. pete Guest

    wrote:
    >
    > Hi I am new to this forum.
    >
    > I have taken a class in C some time ago but now I am reading Kernigan
    > and Richie's book to refresh my knowledge. I think I have forgotten
    > alot and there are no solutions to the exercises. Maybe some people
    > here can check through some of my solutions.
    >
    > Below is a solution to the exercise 1.8.
    >
    > /* blank counter */
    >
    > int x, y, z;


    Globals not needed.

    >
    > void main()


    Nonportable form of main

    > {
    > char c;


    Wrong type for (c). Should be type int.

    >
    > fflush(stdin); // start reading from stdin


    fflush(stdin) is undefined.

    > while(! feof(stdin) ) {
    > switch(c = getchar()) {
    > case ' ' : x++; break;
    > case '\t': y++; break;
    > case '\n': z++;
    > }
    > }
    > fclose(stdin); // clean up


    If you didn't fopen it, you shouldn't fclose it.


    > printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);
    > }




    /* BEGIN blank_counter.c */

    #include <stdio.h>

    int main(void)
    {
    int c;
    unsigned x, y, z;

    x = y = z = 0;
    while ((c = getchar()) != EOF) {
    switch(c) {
    case ' ' : x++; break;
    case '\t': y++; break;
    case '\n': z++;
    }
    }
    printf("[spaces, tabs, newlines] = [%u, %u, %u]\n", x, y, z);
    return 0;
    }

    /* END blank_counter.c */

    --
    pete
     
    pete, Nov 30, 2007
    #3
  4. Flash Gordon Guest

    wrote, On 30/11/07 21:54:
    > Hi I am new to this forum.
    >
    > I have taken a class in C some time ago but now I am reading Kernigan
    > and Richie's book to refresh my knowledge. I think I have forgotten
    > alot and there are no solutions to the exercises. Maybe some people
    > here can check through some of my solutions.
    >
    > Below is a solution to the exercise 1.8.
    >
    > /* blank counter */
    >
    > int x, y, z;


    Why are these declared at file scope? Unless you have a *very* good
    reason then this is the wrong thing to do.

    > void main()


    Start at the begining of K&R again. You will see that the return type of
    main it int.

    > {
    > char c;
    >
    > fflush(stdin); // start reading from stdin


    Where in K&R does is show flushing stdin? Answer, nowhere, because it is
    has no meaning as far as C is concerned (although a specific
    implementation *could* defined it, but that would only apply to that
    implementation). It is particularly pointless at the start of a program
    anyway.

    Also please don't use // style comments when posting to Usenet. Then can
    cause problems with line wrapping.

    > while(! feof(stdin) ) {


    Incorrect use of feof. Please see the comp.lang.c FAQ at
    http://c-faq.com specifically question 12.2

    > switch(c = getchar()) {


    You should assign the result of getchar to a variable of type int, not
    char, see question 12.1 of the comp.lang.c FAQ.

    > case ' ' : x++; break;
    > case '\t': y++; break;
    > case '\n': z++;


    As a matter of style in my opinion you should have a break on the last
    case as well.

    > }
    > }
    > fclose(stdin); // clean up


    You don't need to close stdin.

    > printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);


    Now that you know that main returns an int you should return an int.
    Either 0 (which means success), EXIT_SUCCESS or EXIT_FAILURE.

    > }


    I've not checked to see if your program would work with these things fixed.
    --
    Flash Gordon
     
    Flash Gordon, Nov 30, 2007
    #4
  5. user923005 Guest

    On Nov 30, 1:54 pm, wrote:
    > Hi I am new to this forum.
    >
    > I have taken a class in C some time ago but now I am reading Kernigan
    > and Richie's book to refresh my knowledge. I think I have forgotten
    > alot and there are no solutions to the exercises. Maybe some people
    > here can check through some of my solutions.
    >
    > Below is a solution to the exercise 1.8.
    >
    > /* blank counter */
    >
    > int x, y, z;
    >
    > void main()
    > {
    > char c;
    >
    > fflush(stdin); // start reading from stdin
    > while(! feof(stdin) ) {
    > switch(c = getchar()) {
    > case ' ' : x++; break;
    > case '\t': y++; break;
    > case '\n': z++;
    > }
    > }
    > fclose(stdin); // clean up
    >
    > printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);
    >
    > }


    #include <stdio.h>
    #include <ctype.h>

    int main(void)
    {
    int c;
    unsigned n = 0;
    do {
    c = getc(stdin);
    if (isspace(c)) n++;
    } while (c != EOF);
    printf("File contains %u space characters.\n", n);
    return 0;
    }
     
    user923005, Nov 30, 2007
    #5
  6. Ian Collins Guest

    wrote:
    > Hi I am new to this forum.
    >
    > I have taken a class in C some time ago but now I am reading Kernigan
    > and Richie's book to refresh my knowledge. I think I have forgotten
    > alot and there are no solutions to the exercises. Maybe some people
    > here can check through some of my solutions.
    >
    > Below is a solution to the exercise 1.8.
    >
    > /* blank counter */
    >
    > int x, y, z;
    >

    One more thing, learn to give variables meaningful names!

    --
    Ian Collins.
     
    Ian Collins, Nov 30, 2007
    #6
  7. CBFalconer Guest

    wrote:
    >
    > I have taken a class in C some time ago but now I am reading
    > Kernigan and Richie's book to refresh my knowledge. I think I have
    > forgotten alot and there are no solutions to the exercises. Maybe
    > some people here can check through some of my solutions.
    >
    > Below is a solution to the exercise 1.8.
    >
    > /* blank counter */


    Missing #include of needed standard headers.
    >
    > int x, y, z;
    >
    > void main()


    Illegal. main returns an int. Say so.

    > {
    > char c;


    This should be an int.

    >
    > fflush(stdin); // start reading from stdin


    Illegal. fflush doesn't operate on input files. // comments are
    not allowed in C90, and you don't have a C99 compiler. And you
    certainly don't want to ignore the first inputs.

    > while(! feof(stdin) ) {


    feof only reflects what happened previously, when getchar return
    EOF to signal either end-of-file or error. Don't do this. Fix the
    whole while loop to revolve around receiving EOF in an int.

    > switch(c = getchar()) {
    > case ' ' : x++; break;
    > case '\t': y++; break;
    > case '\n': z++;
    > }
    > }
    > fclose(stdin); // clean up


    No // comments.

    >
    > printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);


    Boom. variadic function call with no prototype.

    > }


    Boom. Failure to return an int. 0, EXIT_OK, EXIT_FAILURE are
    allowed. The macros are in stdlib.h

    You have work to do. Get these things right early.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Nov 30, 2007
    #7
  8. Jack Klein Guest

    On Fri, 30 Nov 2007 13:54:11 -0800 (PST),
    wrote in comp.lang.c:

    > Hi I am new to this forum.
    >
    > I have taken a class in C some time ago but now I am reading Kernigan
    > and Richie's book to refresh my knowledge. I think I have forgotten
    > alot and there are no solutions to the exercises. Maybe some people
    > here can check through some of my solutions.
    >
    > Below is a solution to the exercise 1.8.
    >
    > /* blank counter */
    >
    > int x, y, z;
    >
    > void main()


    There is no example in either version of K&R that starts with "void
    main()".

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Dec 1, 2007
    #8
  9. Guest

    On Dec 1, 2:54 am, wrote:
    > Hi I am new to this forum.
    >
    > I have taken a class in C some time ago but now I am reading Kernigan
    > and Richie's book to refresh my knowledge. I think I have forgotten
    > alot and there are no solutions to the exercises. Maybe some people
    > here can check through some of my solutions.
    >
    > Below is a solution to the exercise 1.8.
    >
    > /* blank counter */
    >
    > int x, y, z;
    >
    > void main()
    > {
    > char c;
    >
    > fflush(stdin); // start reading from stdin
    > while(! feof(stdin) ) {
    > switch(c = getchar()) {
    > case ' ' : x++; break;
    > case '\t': y++; break;
    > case '\n': z++;
    > }
    > }
    > fclose(stdin); // clean up
    >
    > printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);
    >
    >
    >
    > }- Hide quoted text -
    >
    > - Show quoted text -


    hey thanks ur sorce code.i also think that is correct.then u know
    about linklist,i want ur help.bcoz i want to know about how to sort
    the link list using c language.can u help me?
     
    , Dec 1, 2007
    #9
  10. santosh Guest

    wrote:

    > On Dec 1, 2:54 am, wrote:
    >> Hi I am new to this forum.
    >>
    >> I have taken a class in C some time ago but now I am reading Kernigan
    >> and Richie's book to refresh my knowledge. I think I have forgotten
    >> alot and there are no solutions to the exercises. Maybe some people
    >> here can check through some of my solutions.
    >>
    >> Below is a solution to the exercise 1.8.
    >>
    >> /* blank counter */
    >>
    >> int x, y, z;
    >>
    >> void main()
    >> {
    >> char c;
    >>
    >> fflush(stdin); // start reading from stdin
    >> while(! feof(stdin) ) {
    >> switch(c = getchar()) {
    >> case ' ' : x++; break;
    >> case '\t': y++; break;
    >> case '\n': z++;
    >> }
    >> }
    >> fclose(stdin); // clean up
    >>
    >> printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);
    >>
    >>
    >>
    >> }


    > hey thanks ur sorce code.i also think that is correct.


    It most certainly is not correct. Did you not read all the multiple
    posts correcting his numerous errors?

    > then u know
    > about linklist,i want ur help.bcoz i want to know about how to sort
    > the link list using c language.can u help me?


    Good luck using his help.
     
    santosh, Dec 1, 2007
    #10
  11. CBFalconer Guest

    wrote:
    >

    .... snip ...
    >
    > hey thanks ur sorce code.i also think that is correct.then u know
    > about linklist,i want ur help.bcoz i want to know about how to sort
    > the link list using c language.can u help me?


    ur was a city in ancient times. I (capitalized) is the first
    person pronoun. u has not posted here for several years. bcoz is
    totally unrecognizable. Periods are normally followed by at least
    one blank. First characters in sentences are usually upper case.
    Other (possibly typos) errors are: "linklist" != "link list";
    "sorce" probably means "source".

    Most of this is not a language problem, but pure carelessness. The
    result is, to all practical purposes, unintelligible. Please fix
    your message before posting.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Dec 1, 2007
    #11
  12. James Fang Guest

    On Dec 1, 5:54 am, wrote:
    > Hi I am new to this forum.
    >
    > I have taken a class in C some time ago but now I am reading Kernigan
    > and Richie's book to refresh my knowledge. I think I have forgotten
    > alot and there are no solutions to the exercises. Maybe some people
    > here can check through some of my solutions.
    >
    > Below is a solution to the exercise 1.8.
    >
    > /* blank counter */
    >
    > int x, y, z;
    >
    > void main()
    > {
    > char c;
    >
    > fflush(stdin); // start reading from stdin
    > while(! feof(stdin) ) {
    > switch(c = getchar()) {
    > case ' ' : x++; break;
    > case '\t': y++; break;
    > case '\n': z++;
    > }
    > }
    > fclose(stdin); // clean up
    >
    > printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);
    >
    > }


    I have a question:

    If you use the keyboard as the stdin source, how can the situation :
    ((c = getchar()) == EOF) met?

    My perception is that you have to redirect your stdin to a file and
    then run this program.

    Thanks!
     
    James Fang, Dec 2, 2007
    #12
  13. santosh Guest

    James Fang wrote:

    > On Dec 1, 5:54 am, wrote:
    >> Hi I am new to this forum.
    >>
    >> I have taken a class in C some time ago but now I am reading Kernigan
    >> and Richie's book to refresh my knowledge. I think I have forgotten
    >> alot and there are no solutions to the exercises. Maybe some people
    >> here can check through some of my solutions.
    >>
    >> Below is a solution to the exercise 1.8.
    >>
    >> /* blank counter */
    >>
    >> int x, y, z;
    >>
    >> void main()
    >> {
    >> char c;
    >>
    >> fflush(stdin); // start reading from stdin
    >> while(! feof(stdin) ) {
    >> switch(c = getchar()) {
    >> case ' ' : x++; break;
    >> case '\t': y++; break;
    >> case '\n': z++;
    >> }
    >> }
    >> fclose(stdin); // clean up
    >>
    >> printf("[spaces, tabs, newlines] = [%d, %d, %d]\n", x, y, z);
    >>
    >> }

    >
    > I have a question:
    >
    > If you use the keyboard as the stdin source, how can the situation :
    > ((c = getchar()) == EOF) met?
    >
    > My perception is that you have to redirect your stdin to a file and
    > then run this program.


    No. Most systems have a key sequence that can signal end-of-file. Under
    UNIX it is CTRL-D and under Windows, CTRL-Z. The sequence may have to
    appear on it's own line to be effective.
     
    santosh, Dec 2, 2007
    #13
  14. James Fang said:

    <snip>

    > If you use the keyboard as the stdin source, how can the situation :
    > ((c = getchar()) == EOF) met?


    You have just asked (a very slight variant of) comp.lang.c FAQ 12.1b - see
    http://c-faq.com for the answer.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Dec 2, 2007
    #14
    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. John Hoge
    Replies:
    2
    Views:
    605
    John Hoge
    May 23, 2004
  2. tfs
    Replies:
    1
    Views:
    393
    John Saunders
    Jun 28, 2004
  3. John A Grandy
    Replies:
    2
    Views:
    2,429
    Eliyahu Goldin
    Nov 21, 2004
  4. Peter Blatt
    Replies:
    0
    Views:
    451
    Peter Blatt
    Sep 25, 2004
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    213
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page