Thread: Word Count example in Book by K & R

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Bangalore, India
    Posts
    12

    Word Count example in Book by K & R

    This is the code for counting characters, words and line in C program. (Pg 20, K&R)Why do we really need the variable "state" or why is it important to know if you are inside the word or outside the word.

    Is it not enough if you increase the counter for word whenever you encounter
    space, tab or new line?
    Code:
    #include <stdio.h>
    
    #define IN 1 /*inside a word"
    #define OUT 0 /*outside a word"
    
    /* count lines, words, and characters in input */
    
    int main()
    {
       int c, nl, nw, nc, state;
       state = OUT;
       nl = nw = nc = 0;
    
       while ((c = getchar()) != EOF) {
          ++nc;
          if (c == '\n')
             ++nl;
          if (c == ' ' || c == '\n' || c == '\t')
             state = OUT;
          else if (state == OUT) {
             state = IN;
             ++nw;
          }
       }
       printf("%d %d %d\n", nl, nw, nc);
       return 0;
    }
    Thank You.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    "How     many     words in  this line?"

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well it depends on what your input file will look like. If you are guaranteed that your words are separated by one "space" (whatever character that may be) and one space only then yes. But if you have "The house____ is____ blue" as input your count would be wrong.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Location
    Bangalore, India
    Posts
    12
    thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding the word count program from K&R book
    By alter.ego in forum C Programming
    Replies: 11
    Last Post: 06-13-2011, 04:55 AM
  2. Uppercase/Lowercase/Word COunt/Char count
    By Charak in forum C Programming
    Replies: 7
    Last Post: 02-23-2011, 08:16 AM
  3. word count help?
    By n3cr0_l0rd in forum C Programming
    Replies: 13
    Last Post: 02-07-2009, 08:29 AM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

Tags for this Thread