Counting blanks

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rajash@thisisnotmyrealemail.com

    #1

    Counting blanks

    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);
    }
  • Tor Rustad

    #2
    Re: Counting blanks

    rajash@thisisno tmyrealemail.co m 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 <bwzcab@wvtqvm. vw | tr i-za-h a-z>

    Comment

    • pete

      #3
      Re: Counting blanks

      rajash@thisisno tmyrealemail.co m 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

      Comment

      • Flash Gordon

        #4
        Re: Counting blanks

        rajash@thisisno tmyrealemail.co m 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

        Comment

        • user923005

          #5
          Re: Counting blanks

          On Nov 30, 1:54 pm, raj...@thisisno tmyrealemail.co m 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;
          }

          Comment

          • Ian Collins

            #6
            Re: Counting blanks

            rajash@thisisno tmyrealemail.co m 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.

            Comment

            • CBFalconer

              #7
              Re: Counting blanks

              rajash@thisisno tmyrealemail.co m 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

              Comment

              • Jack Klein

                #8
                Re: Counting blanks

                On Fri, 30 Nov 2007 13:54:11 -0800 (PST),
                rajash@thisisno tmyrealemail.co m 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.l earn.c-c++

                Comment

                • abhiman.max@gmail.com

                  #9
                  Re: Counting blanks

                  On Dec 1, 2:54 am, raj...@thisisno tmyrealemail.co m 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?

                  Comment

                  • santosh

                    #10
                    Re: Counting blanks

                    abhiman.max@gma il.com wrote:
                    On Dec 1, 2:54 am, raj...@thisisno tmyrealemail.co m 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.

                    Comment

                    • CBFalconer

                      #11
                      Re: Counting blanks

                      abhiman.max@gma il.com 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

                      Comment

                      • James Fang

                        #12
                        Re: Counting blanks

                        On Dec 1, 5:54 am, raj...@thisisno tmyrealemail.co m 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!

                        Comment

                        • Richard Heathfield

                          #13
                          Re: Counting blanks

                          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

                          Comment

                          • santosh

                            #14
                            Re: Counting blanks

                            James Fang wrote:
                            On Dec 1, 5:54 am, raj...@thisisno tmyrealemail.co m 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.

                            Comment

                            Working...