This works for me
Code:
#include <stdio.h>
#include <string.h>
#define LEN_TOTAL 21
int main()
{
int word_length_occurrence[ LEN_TOTAL ] = { 0 };
char string[ 700 ] = { 0 },
tempStr[ 300 ] = { 0 },
*tokenPtr;
size_t i, numLines,
maxLength = 0;
printf( "Number of lines: " );
scanf( "%d", &numLines );
gets( tempStr ); /* grabs the carriage return */
for ( i = 0; i < numLines; i++ ) {
fgets( tempStr, sizeof( tempStr ), stdin );
strcat( string, tempStr );
}
tokenPtr = strtok( string, " \n" );
while ( tokenPtr != NULL ) {
maxLength = strlen( tokenPtr );
word_length_occurrence[ maxLength ]++;
tokenPtr = strtok( NULL, " \n" );
}
printf( "%8s%15s\n", "Word Length", "Occurrences" );
for ( i = 1; i < LEN_TOTAL; i++ )
printf( "%4d%15d%c", i, word_length_occurrence[ i ], '\n' );
printf( "\n" );
getchar();
return 0;
}
-Prelude