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 >
Wrap
C/C++ Source or Header
|
2005-06-16
|
1KB
|
43 lines
/* ptrarray.c
*
* Synopsis - Accepts a line of text as input from the key-
* board. Finds the individual words in the input
* text, counts them, displays the count, and
* displays the words in reverse order.
*
* Objective - Illustrates use of an array of pointers to char.
*/
/* Include Files */
#include <stdio.h>
#include <string.h>
/* Constant Definitions */
#define BUF_SIZE 512
#define NUM_WORDS 50
int main( void )
{
char instring[BUF_SIZE];
char *words[NUM_WORDS], /* Note 1 */
*current;
int i = 1;
printf( "Enter text with words delimited by blanks:\n" );
fgets( instring, BUF_SIZE, stdin );
instring[ strlen( instring ) - 1 ] = '\0';
words[0] = current = instring; /* Note 2 */
/* Note 3 */
while ( ( current = strchr( current, ' ' )) != NULL ) {
*current++ = '\0';
words[i++] = current; /* Note 4 */
}
printf( "There were %d words in that line.\n", i );
printf( "In reverse order they are :\n" );
for ( --i; i >= 0; i-- )
printf( "%s\n", words[i] ); /* Note 5 */
return 0;
}