Like Tree1Likes
  • 1 Post By Sean Wilson

BEGINNER - word count C Program explanation

This is a discussion on BEGINNER - word count C Program explanation within the C Programming forums, part of the General Programming Boards category; Making my way through my first C Programming book and I have a solid understanding of the purpose and result ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    3

    BEGINNER - word count C Program explanation

    Making my way through my first C Programming book and I have a solid understanding of the purpose and result of every line in the code with the exception of this section:

    Code:
    Line 1.  while ( *line != kZeroByte ) { 
    Line 2.     if ( ! isspace( *line ) ) { 
    Line 3.          if ( ! inWord ) { 
    Line 4.               numWords++; 
    Line 5.               inWord = true;
                     }
                }
                else
                     inWord = false; 
    }
    from this word count code:

    Code:
    #include <stdio.h>
    #include <ctype.h> 
    #include <stdbool.h>
    
    #define kMaxLineLength    200  
    #define kZeroByte                0 
    
    void    ReadLine( char *line ); 
    int        CountWords( char *line ); 
    
    int main (int argc, const char * argv[]) {
        char      line[ kMaxLineLength ];
        int        numWords;
        
        printf( "Type a line of text, please:\n" );
        
        ReadLine( line );
        numWords = CountWords( line );
        
        printf( "\n---- This line has %d word", numWords );
        
            if ( numWords != 1 )
                 printf( "s" );
        
        printf( " ----\n%s\n", line );
        
        return 0;
    }
    
    void    ReadLine( char *line ) {
        int     numCharsRead = 0;
        
        while ( (*line = getchar()) != '\n' ) {
            line++;
            if ( ++numCharsRead >= kMaxLineLength-1 )
                break;
        }
        *line = kZeroByte;
    }
    
    int    CountWords( char *line ) {
        int        numWords, inWord;
        
        numWords = 0;
        inWord = false;
        while ( *line != kZeroByte ) { 
            if ( ! isspace( *line ) ) { 
                if ( ! inWord ) { 
                    numWords++; 
                    inWord = true;
                }
            }
            else
                inWord = false; 
                line++; //increments the line array to the next character to start the loop again.
        }
        return numWords;
    }
    Here is my confusion. As I read the section of code that is confusing me I would guess that numWords++ would increment the number of characters in the string and not the number of words. Here is my plain english translation:

    Code:
    Line 1.  while the value of line[x] is not equal to zero
    Line 2.     if the value of line[x] is not 'white-space'
    Line 3.          if the previous if statement is true
    Line 4.               increment numWords;
    Line 5.               change inWord from false to true;
    
    with x = to the position in the char position in the line array
    Now, I feel like I have a solid understand of pointers, arrays, and increments but for the life of me, I cannot figure out why 'numWords++' increments the word count.

    If I am cycling through each line[x] and if it is not a 'white-space' character, i will increment numWords, why does that not count the characters.

    Example:

    Line[array] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    char value I N G O D W E T R U S T

    numWords = 0
    the value of line[1] is not equal to zero
    the value of line[1] is not 'white-space' (TRUE)
    Since the previous if value is TRUE
    increment numWords;
    change inWord from false to true;
    numWords now equals 1


    The value of line[2] is not equal to zero
    the value of line[2] is not 'white-space' (TRUE)
    since the previous if value is TRUE
    increment numWords;
    change inWord from false to true;
    numWords now equals 2


    Someone please give me some hints as to where my logic is taking the hit.

    Thanks, Sean
    Last edited by Sean Wilson; 12-07-2011 at 11:47 AM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    In your function CountWords() you're not incrementing the pointer.
    numWords counts the number of words.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    Quote Originally Posted by nonoob View Post
    In your function CountWords() you're not incrementing the pointer.
    numWords counts the number of words.
    nonoob, thanks for the response, but unfortunately I am still at a loss. So, how is numWords actually counting the number of words? For example if I enter two words: HOLY COW
    how does the code written above see HOLY as one word and COW as another.

    To me it looks like it sees each letter as a word not including the 'white-space'.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    The key to the logic is the lines
    Code:
    if ( ! inWord )
      inWord = true ;

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    Tclausex is right. If inWord is false and it encounters a non whitespace it counts it as a word.
    Last edited by mnd22; 12-07-2011 at 12:45 PM.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by nonoob View Post
    In your function CountWords() you're not incrementing the pointer.
    numWords counts the number of words.
    line 55.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    Quote Originally Posted by Tclausex View Post
    The key to the logic is the lines
    Code:
    if ( ! inWord )
      inWord = true ;
    Ok, picture a light bulb above my head.

    So the function CountWord() initially declares inWord = false. Then when it finds a 'non white-space' character, it sets inWord to true and then increments numWords. Then it continues stepping through the array looking for a 'white-space' character that marks the end of the current word. Once this 'white-space is found, inWord is set back to false and the while loop continues.

    Walla.

    Tclausex, thanks for hint that light the fire.

    Sean
    dennis.cpp likes this.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    I think you are making it a little harder then it needs to be.

    How the thing counts the words is it checks each element of the string

    Code:
    0123456789 - string element
    IN GOD WE - actual string
    It starts at element 0 and cycles through the string, and when it finds a white space, then it found a "word."

    Below is what you want to do,


    Since I saw that you are doing line counts... you want to look for the '\n'

    Code:
    //somewhere here the flag for the word is set to false
    
    while (_have_input_ != EOF) //this way it runs to the end of the file
    {
      //if (_is_chaacterr_  = '\n')  //if and only if you actually need a line count.
        //increase line count
    
      //if (test for white space)
        //flag that you have white space
    
      //if (test that you have that flag)
        //increase word count
        //reset the flag
    }
    If you cycle through "IN GOD WE " when it hits element 0 (I of IN), the test for the white space and the flag are both false and it moves on to the next character, and it does the test again for element 1 (N of IN), and again that is false.

    Now on the third run it hits the white space, and the first test is now true and sets the flag, and it goes to the next test (flag test), and that is true, so a word was found and resets the flag.

    And look at what you did.

    What is different?


    Code:
    while ( *line != kZeroByte ) 
    { 
      if ( ! isspace( *line ) ) 
      { 
        if ( ! inWord ) 
        {
          numWords++; 
          inWord = true;
         }
        }
        else
          inWord = false; 
          line++
    Last edited by Strahd; 12-08-2011 at 01:50 AM.

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. Unique Word count program
    By vjefcoat in forum C Programming
    Replies: 3
    Last Post: 11-22-2010, 05:51 PM
  3. Word count program
    By Jpeg6 in forum C Programming
    Replies: 1
    Last Post: 10-18-2010, 10:34 PM
  4. word count program not working
    By vsovereign in forum C Programming
    Replies: 5
    Last Post: 06-04-2010, 02:11 PM
  5. word count program need a bit of help!
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 04-19-2002, 08:15 PM

Tags for this Thread


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