home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch9 / ptrarray.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  1KB  |  43 lines

  1. /*                    ptrarray.c
  2.  *
  3.  *   Synopsis  - Accepts a line of text as input from the key- 
  4.  *               board. Finds the individual words in the input
  5.  *               text, counts them, displays the count, and 
  6.  *               displays the words in reverse order.
  7.  *
  8.  *   Objective - Illustrates use of an array of pointers to char.
  9.  */
  10.  
  11. /* Include Files */
  12. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. /* Constant Definitions */
  16. #define BUF_SIZE     512
  17. #define NUM_WORDS    50
  18.  
  19. int main( void )
  20. {
  21.      char instring[BUF_SIZE];
  22.      char *words[NUM_WORDS],                               /* Note 1 */
  23.           *current;
  24.      int  i = 1;
  25.  
  26.      printf( "Enter text with words delimited by blanks:\n" );
  27.      fgets( instring, BUF_SIZE, stdin );
  28.      instring[ strlen( instring ) - 1 ] = '\0';
  29.  
  30.      words[0] = current = instring;                       /* Note 2 */
  31.                                                           /* Note 3 */
  32.      while ( ( current = strchr( current, ' ' )) != NULL ) {
  33.           *current++ = '\0';
  34.           words[i++] = current;                           /* Note 4 */
  35.      }
  36.  
  37.      printf( "There were %d words in that line.\n", i );
  38.      printf( "In reverse order they are :\n" );
  39.      for ( --i; i >= 0; i-- )
  40.           printf( "%s\n", words[i] );                     /* Note 5 */
  41.      return 0;
  42. }
  43.