New learner Help!

This is a discussion on New learner Help! within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void) { int blanks, tabs, newlines; int c; int done = 0; int lastchar = 0; ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    6

    New learner Help!

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int blanks, tabs, newlines;
      int c;
      int done = 0;
      int lastchar = 0;
    
      blanks = 0;
      tabs = 0;
      newlines = 0;
    
      while(done == 0)
      {
        c = getchar();
    
        if(c == ' ')
          ++blanks;
    
        if(c == '\t')
          ++tabs;
    
        if(c == '\n')
          ++newlines;
    
        if(c == EOF)
        {
          if(lastchar != '\n')
          {
            ++newlines; /* this is a bit of a semantic stretch, but it copes
                         * with implementations where a text file might not
                         * end with a newline. Thanks to Jim Stad for pointing
                         * this out.
                         */
          }
          done = 1;
        }
        lastchar = c;
      }
    
      printf("Blanks: %d\nTabs: %d\nLines: %d\n", blanks, tabs, newlines);
      return 0;
    }

    I type this programming in cygwin using nano -w count.c
    Then I compile it using gcc -o count count.c
    Can I know how to test the programming? Usually I use ./count to test. But this time it is not working.

    Thanks in advance

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    ./count should work. You'd obviously have to type something in, and eventually press CTRL-D to end the input, as you are expecting the file to end with EOF.

    Aside from that:
    Code:
          if(lastchar != '\n')
          {
            ++newlines; /* this is a bit of a semantic stretch, but it copes
                         * with implementations where a text file might not
                         * end with a newline. Thanks to Jim Stad for pointing
                         * this out.
                         */
          }
    Not sure if that's correct - I guess it depends on what you want to achieve, I suppose.

    And I would suggest using a switch() instead of chains of if, or at the very least use "else if" to avoid comparing things that you have already seen that it can not be - e.g. if the character is a tab, it obviously isn't a newline, so no need to do if (c == '\n') ... .


    Edit: And, perhaps this can be simplified
    Code:
      int blanks, tabs, newlines;
      int c;
      int done = 0;
      int lastchar = 0;
    
      blanks = 0;
      tabs = 0;
      newlines = 0;
    in this way:
    Code:
      int blanks = 0;
      int tabs = 0;
      int newlines = 0;
      int c;
      int done = 0;
      int lastchar = 0;
    --
    Mats
    Last edited by matsp; 06-02-2008 at 09:04 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    Yeah, I do not know ctrl-D. Thank you, Mats.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for the learner
    By inmaterichard in forum Game Programming
    Replies: 3
    Last Post: 08-16-2007, 12:55 AM
  2. 2 Noobish Questions from a 2nd Day Learner
    By Bucket in forum C++ Programming
    Replies: 18
    Last Post: 08-25-2006, 01:20 PM
  3. Learner...
    By Hugo716 in forum C++ Programming
    Replies: 13
    Last Post: 04-21-2006, 03:04 PM
  4. Language Poll
    By unanimous in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-23-2002, 02:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21