Learn C on the Mac by Apress

This is a discussion on Learn C on the Mac by Apress within the C Programming forums, part of the General Programming Boards category; Ok, I've been banging my head on an example for the last one hour and I can't seem to find ...

  1. #1
    Registered User shazwi's Avatar
    Join Date
    May 2010
    Posts
    4

    Learn C on the Mac by Apress

    Ok, I've been banging my head on an example for the last one hour and I can't seem to find any way to comprehend the example nor modify the code so that it looks a bit more understandable.

    Basically the whole program is a basic one, for those who has this book, it's in Chapter 8 called wordCount.xcodeproj. Program prompts you to type a sentence, stores the sentence characters in an array, then it counts the words and gives it back the number.

    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:" );
    	
    	ReadLine( line );
    	numWords = CountWords( line );
    	
    	printf( "---- 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++;
    	}
    	
    	return numWords;
    }
    THE PROBLEM.

    Code:
    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++;
    	}
    	
    	return numWords;
    }
    Example states that inWord is true if a letter is encountered.

    The whole part of the while loop is very fuzzy. Can anyone explain?

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    It loops through the entire string searchgin for a space. Instead of increasing the index of the current position each time it increases the pointer, which is essentially the same thing. kZeroByte is the NUL character (you shouldn't use a macro for that), which terminates every string. So each time it encounters a space, it sets a flag marking the end of a word, and if a new word begins, it increases the word count.

  3. #3
    Registered User shazwi's Avatar
    Join Date
    May 2010
    Posts
    4
    Quote Originally Posted by Brafil View Post
    It loops through the entire string searchgin for a space. Instead of increasing the index of the current position each time it increases the pointer, which is essentially the same thing. kZeroByte is the NUL character (you shouldn't use a macro for that), which terminates every string. So each time it encounters a space, it sets a flag marking the end of a word, and if a new word begins, it increases the word count.
    Thanks for replying bro.

    But I checked on a reference site that isspace() returns a non-zero if its argument is space. So in this case, if there is space, it's supposed to increment numWords.

    But they put:
    Code:
    if( ! isspace(*line) )
    instead of:
    Code:
    if( isspace(*line) )
    Which means that if there is a space, isspace returns non-zero, then with the !, it becomes false?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by shazwi View Post
    But they put:
    Code:
    if( ! isspace(*line) )
    instead of:
    Code:
    if( isspace(*line) )
    Which means that if there is a space, isspace returns non-zero, then with the !, it becomes false?
    This is true -- ! changes false to true and vice versa.

    Quote Originally Posted by shazwi View Post
    if there is space, it's supposed to increment numWords.
    This I think is where your problem is, since the statement you gave is false. You don't want to increment numWords on a space. You only want to increment numWords when you find a new word, and new words don't start with a space.

  5. #5
    Registered User shazwi's Avatar
    Join Date
    May 2010
    Posts
    4
    Quote Originally Posted by tabstop View Post
    This is true -- ! changes false to true and vice versa.


    This I think is where your problem is, since the statement you gave is false. You don't want to increment numWords on a space. You only want to increment numWords when you find a new word, and new words don't start with a space.
    OMG... You're awesome! Dang... This book was supposed to be for newbs and easy to understand... Thanks loads tabstop!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to obtain a MAC address from an NIC using C
    By W O in forum C Programming
    Replies: 4
    Last Post: 03-16-2010, 10:02 AM
  2. How to learn everything
    By audinue in forum General Discussions
    Replies: 18
    Last Post: 09-01-2009, 01:29 AM
  3. Novice trying to learn C++
    By dead in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2003, 08:25 PM
  4. Programming Objective C in Mac OSX
    By MacUser in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-17-2002, 08:12 AM
  5. Mac
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 02-20-2002, 03:38 PM

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