How to fix this structure?

Discussion in 'C Programming' started by Chris R., May 17, 2004.

  1. Chris R.

    Chris R. Guest

    Hi everyone. I am trying to finish my homework, but I seem not to
    figure out how to fix this structure that seems to make wrong output.
    What this program should do is use structure to save the word and line
    number on which it was in array pointer using malloc and print them
    out alphabetized (no duplicates).

    Most of it works, except the words seem to be corrupted. Here is a
    code, if anyone can help me it would help so much since I've been
    working on this for a really long time. I tried even changing '\0' to
    NULL, but then program errored out.


    Here is my code:



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


    typedef struct list
    {
    int linenum;
    char word[30];
    } LIST;



    void main()
    {
    LIST *p[1000], *holder;
    int line = 1;
    int pcounter = 0;
    char buffer[40];
    int buffercounter = 0;
    char letter = ' ';
    int inword = 0;
    int i;
    int compare;
    int shouldstore = 1;
    int x,y;


    while (letter != EOF)
    {
    letter = getchar();
    if ( (letter >= 'a' && letter <= 'z') || (letter >=
    'A' && letter <= 'Z') )
    {
    letter = toupper(letter);
    inword = 1;
    buffer[buffercounter] = letter;
    buffercounter++;
    }
    else if (letter == '\n')
    {
    line++;
    if (inword == 1)
    {
    inword = 0;
    buffercounter++;
    buffer[buffercounter] = '\0';
    buffercounter = 0;
    if (pcounter == 0)
    {
    p[pcounter] = (LIST*) malloc (sizeof(LIST));
    strcpy( p[pcounter]->word , buffer);
    p[pcounter]->linenum = line;
    pcounter++;
    }
    else
    {
    for (i = 0; i < pcounter; i++)
    {
    compare = strcmp(p->word, buffer);
    if (compare == 0)
    {
    shouldstore = 0;
    }
    }
    if (shouldstore == 0)
    {
    shouldstore = 1;
    }
    else
    {
    p[pcounter] = (LIST*) malloc (sizeof(LIST));
    strcpy( p[pcounter]->word, buffer);
    p[pcounter]->linenum = line;
    pcounter++;
    }
    }

    }
    else
    {
    buffercounter = 0;
    }
    }
    else
    {
    if (inword == 1)
    {
    inword = 0;
    buffercounter++;
    buffer[buffercounter] = '\0';
    if (pcounter == 0)
    {
    p[pcounter] = (LIST*) malloc (sizeof(LIST));
    strcpy( p[pcounter]->word , buffer);
    p[pcounter]->linenum = line;
    pcounter++;
    }
    else
    {
    for (i = 0; i < pcounter; i++)
    {
    compare = strcmp(p->word, buffer);
    if (compare == 0)
    {
    shouldstore = 0;
    }
    }
    if (shouldstore == 0)
    {
    shouldstore = 1;
    }
    else
    {
    p[pcounter] = (LIST*) malloc (sizeof(LIST));
    strcpy( p[pcounter]->word, buffer);
    p[pcounter]->linenum = line;
    pcounter++;
    }
    }
    }
    else
    {
    buffercounter = 0;
    }
    }


    }

    for(x = 0; x < pcounter; x++)
    for(y = 0; y < pcounter-1; y++)
    {
    compare = strcmp(p[y]->word, p[y+1]->word);
    if( compare < 0)
    {
    holder = p[y+1];
    p[y+1] = p[y];
    p[y] = holder;
    }
    }

    for (x = 0; x < pcounter; x++)
    {
    printf("\nThe word %s was on line %d", p[x]->word, p[x]->linenum);
    }
    }
    Chris R., May 17, 2004
    #1
    1. Advertising

  2. Chris R.

    Al Bowers Guest

    Chris R. wrote:
    > Hi everyone. I am trying to finish my homework, but I seem not to
    > figure out how to fix this structure that seems to make wrong output.
    > What this program should do is use structure to save the word and line
    > number on which it was in array pointer using malloc and print them
    > out alphabetized (no duplicates).
    >
    > Most of it works, except the words seem to be corrupted. Here is a
    > code, if anyone can help me it would help so much since I've been
    > working on this for a really long time. I tried even changing '\0' to
    > NULL, but then program errored out.
    >
    >

    .... code snipped ....

    There are many errors, so I want take the time to try to fix your
    code. Along with the errors you have a design problem with the
    date type and with all the operations being done in one function, main.

    I would make the data type List which contains a pointer to an
    array of data type WORD as one member and a counter to keep a count
    on the number of words in the list as the other member. In the
    data type WORD you would store the word in on member and the other
    member would be the line number.

    Example:
    typedef struct WORD
    {
    unsigned linenum;
    char *name;
    } WORD;

    typedef struct LIST
    {
    WORD *word;
    unsigned count;
    } LIST;

    Now you should write fucntions that do simple operations
    on the LIST datatype. For example AddWordtoList() to add
    a word, PrintList() to print the list, FreeList() to free
    the allocations once you are finished with the list.
    You could use Standard C functions bsearch to search the
    list for dupes and function qsort to sort the list as you
    build it.

    An example of a possible definiion of AddWordtoList().

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

    int cmp(const void *v1, const void *v2)
    {
    const WORD *w1 = v1;
    const WORD *w2 = v2;

    return strcmp(w1->name,w2->name);
    }

    int AddWordtoList(LIST *p, const char *word, unsigned linenr)
    {
    WORD *tmp,key;
    char *s, *s1;

    if((s = malloc(strlen(word)+1)) == NULL) return 0;
    strcpy(s,word);
    for(s1 = s; (*s1 = tolower((unsigned char)*s1));s1++) ;
    key.name = s;
    if(p->word != NULL)
    if(bsearch(&key,p->word,p->count,sizeof *p->word,cmp))
    { /* dupe */
    free(s);
    return 0;
    }
    tmp = realloc(p->word,(p->count+1)*(sizeof *tmp));
    if(tmp == NULL)
    {
    free(s);
    return 0;
    }
    p->word = tmp;
    p->word[p->count].name = s;
    p->word[p->count++].linenum = linenr;
    qsort(p->word,p->count,sizeof *p->word,cmp);
    return 1;
    }


    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
    Al Bowers, May 18, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Philippot
    Replies:
    0
    Views:
    455
    Patrick Philippot
    Apr 14, 2006
  2. Xah Lee
    Replies:
    22
    Views:
    1,116
    Tim Roberts
    Mar 21, 2006
  3. Chris R.

    How to fix this structure?

    Chris R., May 17, 2004, in forum: C Programming
    Replies:
    18
    Views:
    487
    Richard Bos
    May 26, 2004
  4. Xah Lee
    Replies:
    23
    Views:
    1,061
    Tim Roberts
    Mar 21, 2006
  5. Xah Lee
    Replies:
    21
    Views:
    781
    Tim Roberts
    Mar 21, 2006
Loading...

Share This Page