Thread: Program to show the number of occurrences of words...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Program to show the number of word lengths

    Hi,

    This is a program to take in several lines of text and output the number of one-letter word, two-letter words.....etc. But it didn't work. I think it's because the last word of the sentence got stuck to the first word of the next sentence and become one word. I tried adding a space in between ( maybe i did it the wrong way ) but still didnt' output corectly. Pls give a hand. Thnx in advance.!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       int word_length_occurrence[ 20 ] = { 0 };
       char string[ 700 ];
       char tempStr[ 300 ];
       int i, numLines;
       int maxLength = 0;
       char *tokenPtr;
    
       printf( "Number of lines: " );
       scanf( "%d", &numLines );
    
       gets( tempStr ); /* grabs the carriage return */
    
       for ( i = 0; i < numLines; i++ ) {
          gets( tempStr );
          strcat( string, tempStr );
       }
    
       tokenPtr = strtok( string, " " );
    
       while ( tokenPtr != NULL ) {
          word_length_occurrence[ strlen( tokenPtr ) ]++;
          if ( maxLength < strlen( tokenPtr ) )
             maxLength = strlen( tokenPtr );
          tokenPtr = strtok( NULL, " " );
       }
    
       printf( "%8s%15s\n", "Word Length", "Occurrences" );
    
       for ( i = 1; i <= maxLength; i++ )
          printf( "%4d%15d%c", i, word_length_occurrence[ i ], '\n' );
    
       printf( "\n" );
       system("PAUSE");
       return 0;
    }
    Last edited by Nutshell; 01-25-2002 at 03:37 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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
    My best code is written with the delete key.

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    k

    but what is wrong with my version? Can i not use fgets() ?
    Last edited by Nutshell; 01-26-2002 at 02:44 AM.

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    And why use the " \n" as the delimiter for strtok ?

    thnx

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Here's the deal, suppose you have the following input...
    3 lines

    This is a text.
    This is only a text.
    Thank you.

    gets() converts the '\n' at the end of each of these into a '\0'.
    fgets() doesn't change the '\n', it puts the '\0' after the '\n'.

    So, when you are all done and strcat these together, you end up with...
    Code:
    "This is a text."
    "This is only a text."
    "Thank you."
    becomes...
    "This is a text.This is only a text.Thank you."
    
    or if you use fgets, it becomes....
    "This is a text.\n"
    "This is only a text.\n"
    "Thank you.\n"
    becomes...
    "This is a text.\nThis is only a text.\nThank you.\n"
    In the latter, if we use '\n' as well as ' ' as a token delimiter, then it clearly divides the words well. In the former, since we've tossed the '\n', there isn't any way to turn the final string back into words where the '\n' would be.

    To use multiple token delimiters, we just put multiple characters in the string for strtok...
    tokenPtr = strtok( string, " \n" );

    Finally, I ran into some problems with your display routine. Since your array of word length occourances is only 20, you have to make sure you don't print values for any lengths greater than 20.
    Callou collei we'll code the way
    Of prime numbers and pings!

  6. #6
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    oh i understand now. Coz my book doesn' t teach me how to use multiple-delimiters, so i thought " \n" is one whole delimiter.

    thnx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Number of words multiplied by the number of lines in Linux
    By sjalesho in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 03:25 PM