Thread: Word Counting program not understanding

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    India ,Kerala
    Posts
    37

    Word Counting program not understanding

    Hello Masters ,
    I am learning C programming from K & R text .
    i dont understand the wordcounting programing .
    How is working ?
    please help me
    Code:
    #include<stdio.h>
    
     #define IN 1
     #define OUT 0
     main()
    {
     int c,n1,nw,nc,state;
      state = OUT;
     n1 =  nw = nc = 0;
       while((c =getchar()) != EOF )
       { 
                ++nc;
         if(c == '\n')
                ++n1;
       if(c == ' ' || c == '\n' || c == '\t')
          state = OUT;
      else if(state == OUT)
            state = IN;
            ++nw;                 // How to show it 0 a when I exit without press any key ?
           }
           }
    printf("%d %d %d\n",n1 ,nw,nc);
    }
    How did here use the sate ,IN ,OUT ?
    any explain the progaram
    thanks in advance
    Last edited by thannara123; 08-19-2013 at 08:27 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I am not a master, hope you accept answers from a simple human. (kidding)

    Hope you know how define works.
    Code:
    #define IN 1
    Whenever you see IN in the code, after this line, it will be replaced by the value 1. It's a synonymous.

    I do not understand the comment-question you have made.

    The while loop, reads one character after another.
    For example, let me input my name( Samaras ) and then hit enter. Then I also will hit the EOF signal.
    What will happen?
    First, 'S' will be fetched by getchar and be assigned into c. nc will be increased.
    The line 13 if will check if 'S' is the newline, no it is not.
    Then it will check in the line 15 if, if c is a whitespace, a newline(that's the enter) or a tab? 'S' is not one of them.
    Then, we enter the else if, because of line 8 and we assign IN into state and increase the cw by one.

    Then, getchar will fetch 'a' and things will go as before, but now state has the value IN, thus it will not enter the if else.

    When getchar fetches the newline than I inputted after my name, it will enter line 13 if, increase the n1 counter.
    Then, it will go in line 15 if, enter the body of this if, assign OUT into state and that's all, because now we go to the getchar which fetches the EOF and we exit the loop and print the counters.

    As you can see, by running the code in paper, cw got increased once, because we had only one word.
    n1 got increased one as well, because we had only one newline (I just hit enter once).
    And nc, will be the number of the characters we inputted. How many are they? They are as many as the letters of my name + the newline character. That is 7 + 1 = 8.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Mar 2011
    Location
    India ,Kerala
    Posts
    37
    Thanks , am clear ...
    whenever a character type ,this program first decide it is a word and set state = IN(till the white space newline ,\t,) (i was expect a reverse style )..

    sir i have other doubts ..?

    in if cases there is no curly brackets then how the statement works ?
    only the first line under the condition ?(my guess)
    please explain ?

    sorry for my bad english

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Your English is not so bad and by practicing they will get better. Actually posting in this post, was a great help for my English and learning the terminology of our science and when I had a scholarship in Switzerland, the previous year, I had no problem at all.

    Yes, your guess is correct.

    Example.
    Code:
    int a = 5;
    if(a == 5)
        printf("When curly brackets are missing, only the first line of code after the if statement is the body of this if!\n");
    printf("This printf is not part of the a == 5 if statement.\n");
    Example no.2
    Code:
    int c = 0;
    if( c == 1 )
        printf("Printf1\n");
    printf("printf2\n");
    Example no.3
    Code:
    int g = 10;
    if(g == 10)
    /* I am a comment */
         printf("printf1\n");
    printf("printf2\n");
    Try to think what the output of these examples will be and then run them.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Mar 2011
    Location
    India ,Kerala
    Posts
    37
    Thanks

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by thannara123 View Post
    How does the program use the state ,IN ,OUT?
    The program considers ' ', '\n', or '\t' as not being part of a word and sets the state to OUT, otherwise, it sets the state to IN or leaves the state set to IN if it's already set to IN, which indicates that the program is currently reading the characters of a word. The state only changes from OUT to IN for the first character of a word, and that is when the word counter is incremented.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Location
    India ,Kerala
    Posts
    37
    thanks the reply i understood sir

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help understanding Histogram Word Count Program
    By piratemonkey247 in forum C Programming
    Replies: 4
    Last Post: 01-18-2013, 05:14 AM
  2. 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
  3. Word Counting Program
    By psv1120 in forum C Programming
    Replies: 11
    Last Post: 02-23-2011, 03:57 PM
  4. word counting program
    By mashour06 in forum C Programming
    Replies: 2
    Last Post: 06-09-2009, 03:58 AM
  5. Word Counting Program Help
    By rdave1 in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2005, 04:30 PM