home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / sys / mac / programm / 18298 < prev    next >
Encoding:
Text File  |  1992-11-11  |  1.7 KB  |  72 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!pageworks.com!world!djk
  3. From: djk@world.std.com (Dan J Keldsen)
  4. Subject: A simple question from a beginner.
  5. Message-ID: <BxLGCH.AAt@world.std.com>
  6. Summary: I have a problem from "Learn C on the Mac"
  7. Keywords: simple C youKnowTheAnswer help
  8. Organization: The World @ Software Tool & Die
  9. Date: Thu, 12 Nov 1992 08:17:04 GMT
  10. Lines: 60
  11.  
  12. Hi folks,
  13.  
  14. I picked up "Learn C on the Macintosh" and am having a problem with modifying the "wordCount.c" code to not only analyze the # of words typed, but to print them out on separate lines (one word to a line) whenever white space is found. Something about my code just "ain't happenin'". I get the right number of characters printed, but the characters aren't what they should be (something like "car" turning into "***" - the "*" character not actually being a "*"). 
  15.  
  16. HELP? Source code follows:
  17. ______-------________-----
  18. #include <stdio.h>
  19.  
  20. #define MAX_LINE_LENGTH    200
  21.  
  22. #define C_RETURN    '\n'
  23. #define C_TAB        '\t'
  24. #define C_SPACE        ' '
  25.  
  26.  
  27. void main( void )
  28. {
  29.     char    line[ MAX_LINE_LENGTH ], *charPtr, inWord;
  30.     int        numWords;
  31.  
  32.     printf( "Type a line of text, please:\n" );
  33.  
  34.     charPtr = line;
  35.     numWords = 0;
  36.     inWord = FALSE;
  37.     
  38.     while ( ( *charPtr = getchar() ) != C_RETURN )
  39.     {
  40.         if ( (*charPtr != C_TAB) && (*charPtr != C_SPACE) )
  41.         {
  42.             if ( ! inWord )
  43.             {
  44.                 inWord = TRUE;
  45.                 printf( "%c", charPtr );
  46.                 numWords++;
  47.             }
  48.             else
  49.                 printf( "%c", charPtr );
  50.         }
  51.         else
  52.             {
  53.             inWord = FALSE;
  54.             printf( "\n" );
  55.             }
  56.         charPtr++;
  57.     }
  58.     
  59.     printf( "\nYou just typed %d word", numWords );
  60.     
  61.     if ( ( numWords > 1 ) || ( numWords == 0 ) )
  62.         printf( "s." );
  63.     else
  64.         printf( "." );
  65. }
  66. ----______------_____------______
  67.  
  68. Dan Keldsen
  69. BERKLEE College o Music - Boston, MA
  70. djk@world.std.com
  71.  
  72.