Thread: Count Number of Letters in a Word

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    4

    Count Number of Letters in a Word

    Hi im pretty new to C programming and im having trouble with this assignment

    Write a C program that reads text from the standard input and prints out a frequency table for the word lengths found in the text.
    Your program should read from standard input and write to standard output.
    Your program should have at least three functions: one that processes a line at a time, one for printing the result table and the main function.
    You can use malloc() or calloc() to create all your arrays and buffers.
    Use fgets() for reading the input.
    Use strtok() for tokenizing. To keep this simple, we will use the string " ,.;:'\"&!? -_\n\t" as the delimiter string for strtok().
    The output should consist of a table of word length followed by frequency. The table should only show the rows for non-zero frequencies. Show the average length as the last line of the output. See the sample output below.
    You can assume that the maximum length of a word is 50 characters.

    mainly my trouble is starting, i cant seem to figure out how to get a program to count the number of letters in a word and that would be the biggest help for me to figure out and help welcome

  2. #2
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    okay, here's a start:

    Code:
    char buffer[1000];
    char *word;
    size_t word_len;
    
    printf("enter block of text, terminate with newline (<enter>):\n");
    fgets(buffer, sizeof buffer, stdin);
    
    word = strtok(buffer, " ");
    while (word) 
    {
        word_len = strlen(word);
        printf("\"%s\" has %d characters\n",word,word_len);
        word = strtok(NULL, " ");
    }

    now go forth and multiply
    Last edited by jephthah; 05-14-2010 at 12:09 AM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So you made a new account, skipped the forum guidelines, just to do this guy's homework for him?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    lol. Quzah you are the best!
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4

    so far

    ive got this

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAXWORD 50 //the max word we will see
    #define MAXLINE 1000 //the max line we will see
    
    
    //Function prototypes
    void processLine(char [] , int [] );
    void printTable(int []);
    
    /**
     * Main function starts the program
     * Reads from the standard input and outputs to
     * the Standard output
     */
    
    
    int main() {
        char line[MAXLINE];
        int counts[MAXWORD];
    
        while (fgets(line, sizeof (line), stdin) != NULL) {
            processLine(line, counts);
        }
        printTable(counts);
        return 0;
    }
    
    /**
     * Processes one line at a time
     */
    void processLine(char line[MAXLINE] , int counts[MAXWORD] ) {
        char *word = strtok(line, ",.;:'\"&!? -_\n\t "; //cuts of word count at these symbols
    	size_t word_len;
    		while (word){
    			word_len = strlen(word);
    			printf("\"%s\" has %d characters\n", word, word_len);
    			word = strtok(NULL, ",.;:'\"&!? -_\n\t ");
    		}
      
    }
    
    /**
     *Prints the Word count table to stdout (use standard printf)
     */
    void printTable(int counts[MAXWORD]) {
    }

    from here whats the best way to count how many times a word has 1, 2, 3, etc.... letters in it so i can print of a frequency chart like this to show how many times a one word letter appears and so forth? im thinking i have to save the number in the count array

    len count
    ================
    1 1680
    2 4677
    3 7263
    4 6131
    5 3669
    6 2110
    7 1700
    8 824
    9 527
    10 245
    11 137
    12 50
    13 20
    14 9
    15 2
    17 1
    ================
    average 4.01
    ================
    Last edited by quiksand; 05-14-2010 at 10:41 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    And now for something completely different - code tags (read about them)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4

    ok

    Quote Originally Posted by Salem View Post
    And now for something completely different - code tags (read about them)
    any particular sites for tutorials on how to use them in my case? or just basic information about them?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Did you miss something on your way in?
    << !! Posting Code? Read this First !! >>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4
    Quote Originally Posted by Salem View Post
    Did you miss something on your way in?
    << !! Posting Code? Read this First !! >>
    should be fixed now sorry about that

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can use an array if you know the maximum possible word length. Say no words are longer than 10 letters. Make an array of 11 elements, set it all to zero, and then increment the slot in the array that corresponds to word length:
    Code:
    numofwordsxlong[ wordlen ] ++;
    If you don't know the maximum possible word length, you could make a list of some kind which stores two things, the word length, and the count, and run through the list. If that length isn't in the list, add a new bucket and set the count to 1 for that bucket, other wise, find the already existing one and increment its count.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    Quote Originally Posted by quzah View Post
    So you made a new account, skipped the forum guidelines, just to do this guy's homework for him?
    i'm familiar with the rules here. just because i finally created an account doesnt mean i haven't been aware of this site for the past decade.

    if you think i "did his homework" you've got a really, really low threshold of what constitutes either "doing", or what constitutes "homework"

    that, or you just didnt really read his OP and apply any critical consideration of my minimalist reply.



    Quote Originally Posted by claudiu View Post
    lol. Quzah you are the best!
    and you must be the offical Site Monkeyboy. Charmed.



    .
    Last edited by jephthah; 05-14-2010 at 11:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game and strcmp
    By crazygopedder in forum C Programming
    Replies: 12
    Last Post: 11-23-2008, 06:13 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Word Count
    By simple in forum C Programming
    Replies: 12
    Last Post: 10-04-2002, 10:47 PM
  4. Replies: 2
    Last Post: 01-02-2002, 02:05 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

Tags for this Thread