Thread: Help with storing and displaying strings in linked list!

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    12

    Help with storing and displaying strings in linked list!

    I'm new to C programming and so I'm having some problems with a simple program I'm trying to code. Essentially I'm trying to read in a text file, store the individual words into a linked list, then display the list (actually, the real goal is to only store each unique word and how many times that word is found, but I'll get to that later...).

    What I've attempted to do is read in each individual character from the file and check if it's alphabetic. If it is, I add the char into a character array until a full word is built. Once a non-alpha character is read, I add the character array to the linked list and reset the array, ready to start reading in the next word.

    The problem I've encountered is when displaying the contents of the list, only the first 8 characters are displayed correctly - the ninth character is always a 'ŕ' and the tenth is a star character. e.g 'disconnect' is displayed as 'disconneá☼'. All words up to 8 characters in length display OK.

    I'm sure my code is full of errors/inefficiencies so if anyone could tell me the areas I'm going wrong it would be greatly appreciated.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    void BuildWord(char c, int i)
    {
        char word[50];
        if ((c == 0) && (i != 0))
        {
           word[i] = '\0';
           AddToList(word);
        }   
        else
            word[i] = c;   
    }
    
    
    int AddToList(char word[])
    {
        typedef struct wordListT
        {
                int entries;
                char words[50];
                struct wordListT *next;
        }
        wordList;
        wordList *head = NULL;
        wordList *current = NULL;
        if (head==NULL)
        {
           head = (wordList *) malloc(sizeof(wordList));       
           head->next = NULL;
           current = head;
        }
        else
        {
            current->next = (wordList *) malloc(sizeof(wordList));
            current = current->next;
            current->next = NULL;
        }
        strcpy(current->words, word);
        
        //current = head;
        while (current != NULL)
        {
              printf("%s ", &current->words);
              current = current->next;
        }
              
    }
    
    
    int main(void)
    {
        char c;
        int i, j = 0;
        FILE *src = fopen("test.txt", "rt");
        for (i = 0; !feof(src); ++i)
        {
            c = fgetc(src);
            if (isalpha(c))
            {            
               tolower(c);
               BuildWord(c,j);
               j++;
            }
            else
            {
                BuildWord(0,j);
                j = 0;
            }
        }        
        fclose(src);
        return 0;
    }

  2. #2
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    //current = head;
    How are you able to display the list with this line being commented?

    Guessing by the looks of it, try to dynamically allocate space for the character array "word" and free it at the end of each iteration. YOu still have characters of the previous iteration left in the array

    and
    printf("%s ", &current->words);
    why use & here ?
    In the middle of difficulty, lies opportunity

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm having some problems with a simple program I'm trying to code
    You're making it more complicated than it should be. The single biggest problem is that you're relying on local variables outside of the function they're defined in. That's a big no-no. Your list really isn't a list either. Life would be so much easier if you'd declare the things you need in main and pass them as necessary:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    typedef struct wordList
    {
      int entries;
      char words[50];
      struct wordList *next;
    } wordList_t;
    
    wordList_t *AddToList ( wordList_t *head, char word[] )
    {
      wordList_t *current = NULL;
    
      if ( head == NULL ) {
        head = malloc ( sizeof *head );       
        head->next = NULL;
      }
      else {
        current = malloc ( sizeof *current->next );
        current->next = head;
        head = current;
      }
    
      strcpy ( head->words, word );
    
      return head;
    }
    
    void PrintList ( wordList_t *head )
    {
      while ( head != NULL ) {
        printf ( "%s ", head->words );
        head = head->next;
      }
    
      printf ( "\n" );
    }
    
    int main ( void )
    {
      char c;
      int i, j = 0;
      char word[50];
      wordList_t *list = NULL;
    
      FILE *src = fopen ( "test.txt", "r" );
    
      for ( i = 0; ( c = fgetc ( src ) ) != EOF; ++i ) {
        if ( isalpha ( c ) )
          word[j++] = tolower ( c );
        else {
          word[j++] = '\0';
          list = AddToList ( list, word );
          j = 0;
        }
      }
    
      PrintList ( list );
      fclose ( src );
    
      return 0;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing Information then Displaying
    By peckitt99 in forum C++ Programming
    Replies: 11
    Last Post: 09-05-2006, 11:29 AM
  2. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  3. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM