Help - how do i define a structure?

This is a discussion on Help - how do i define a structure? within the C Programming forums, part of the General Programming Boards category; I am trying to define a structure called "wordcount" which has 2 fields: a character array of length 64 called ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    13

    Help - how do i define a structure?

    I am trying to define a structure called "wordcount" which has 2 fields: a character array of length 64 called "word" and an integer called "count". How could I do this?

    Also, how would I declare an array of 1000 wordcounts called "wordlist"?

    Thank you!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    struct wordcount
    {
      int count;
      char word[64];
    } wordlist[1000];
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    13

    need help with word count

    Thanks, I am trying to write a function that reads a file of words and populates the array "wordlist" with the words in the file, and the count of how many times each occurs.
    I then have to use this function in a program that prints out the words from the file with the counts.

    e.g. if the file contains "round and round and round we go"
    output is:
    round 3
    and 2
    we 1
    go 1

    I have no idea how to this!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The best way would be a binary tree. Read a word, if that word is not in the tree already then insert it. If the word already exists in the tree then increment a counter at that node. You can then traverse the tree and print the word, then the count at each node. A node would look like this:
    Code:
    struct node
    {
      char word[SIZE];
      int count;
      struct node *left;
      struct node *right;
    };
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    Thanks, I am trying to do it without using a binary tree (thats the next question!). I have some code which i think does a similar job to the program I am trying to create but reads the characters from getch not an input file, and counts keywords:

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>

    #define MAXWORD 100

    int getword(char *, int);
    int binsearch(char *, struct key *, int);

    /*count keywords */
    main()
    {
    int n;
    char word [MAXWORD];

    while (getword(word, MAXWORD) !=EOF)
    if (isalpha(word[0]))
    if ((n=binsearch(word, keytab, NKEYS)) >= 0)
    keytab[n].count++;
    for (n=0; n< NKEYS; n++)
    if (keytab[n].count, keytab[n].word);
    return 0;
    }

    /* binsearch: find word in tab[0]...tab[n-1] */
    int binsearch(char *word, struct key tab[], int n)
    {
    int cond;
    int low, high, mid;

    low = 0;
    high = n -1;
    while (low <= high) {
    mid = (low=high) / 2;
    if ((cond = strcmp(word, tab[mid].word)) < 0)
    high = mid - 1;
    else if (cond > 0)
    low = mid + 1;
    else
    return mid;
    }
    return -1;

    /*getword: get next word or character from input */
    int getword(char *word, int lim)
    {
    int c, getch(void);
    void ungetch(int);
    char *w=word;

    while (isspace(c = getch()))
    ;
    if (c != EOF)
    *w++=c;
    if (!isalpha(c)) {
    *w = '\0';
    return c;
    }
    for ( ; --lim > 0; w++)
    if (!isalnum(*w = getch())) {
    ungetch(*w);
    break;
    }
    *w = '\0';
    return word[0];
    }
    }

    This program counts the occurences of C keywords. How could I modify it so it reads a file of words (lower case letters) and populates the array "wordlist" with the words in the file, and the count of how many times each occurs?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 11:20 PM
  2. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 06:33 AM
  3. Compiling error: Too many arguments.
    By Tuah in forum C++ Programming
    Replies: 16
    Last Post: 06-10-2008, 05:28 PM
  4. Help me with function call
    By NeMewSys in forum C++ Programming
    Replies: 16
    Last Post: 05-22-2008, 02:53 PM
  5. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 07:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21