can someone please help me to write a code in c to do the following:
1)read words from a file
2)store the words in a linked list
3)read the words and display
4)count the number of words in list

We don't do homework. If you post code which you have written upto now, and say where you are having the problems, we will help.

We don't do homework. If you post code which you have written upto now, and say where you are having the problems, we will help.

here is code

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>





struct doc_words
       {
        char words[30];
        struct doc_words *next;
       };

       struct doc_words *head=NULL;;
       struct doc_words *current;
       //struct doc_words *head
       struct doc_words* newNode;

//char *calloc();

struct doc_words *getmem()
       {
        struct doc_words *p;
        //p = malloc(sizeof(struct doc_words));
        p = (struct doc_words*)malloc(sizeof( doc_words));
        if(!p)
              {printf("out of memory");
               return(0);
              }
        else
        return(p);
       }

struct doc_words *AddWords( char text[30])
       {

        newNode=getmem();
       newNode->words[20]= text[30];
        newNode->next=head;
       head=newNode;
       }

void extract_words_from_file()
     {

      char word[30];

      FILE *ifp;

      if ((ifp=fopen("C:\\collins_tirivamwe\\DSA\\text.txt","r"))!=NULL)
         {

      while (fscanf(ifp,"%s",word)!=EOF)

            {
            printf("%s",word);
             AddWords(word);
            }
      fclose(ifp);
      }
      else
        printf("error");
     }

void read()
    {
       printf("words in list");
     struct doc_words *current= head;
     while (current!=NULL)
           {

            printf("%s\n",current->words);
            current=current->next;
           }

    }

int getCount()
    {
     int count=0;
     struct doc_words *current= head;
     while (current!=NULL)
           {
            count++;
            current=current->next;

           }

      printf("\n %d",count);
     return (count);
    }

/*int getCount(char word)
    {
     int count=0;
     struct doc_words *p = head;
     while (p!=NULL)
           {
            if (strcmp(p->word,word)==0)
               {
                count++;
                p=p->next;
               }
            else
                {
                  p=p->next;
                }
     }
     return(count);
    }

   */



int main()
{
      int p=0;
      extract_words_from_file();

      p=getCount();
     // printf("\nnumber of words are %d",p);
read();
      system("PAUSE");
      return 0;
}

Some of the corrected errors are

void AddWords( char text[30]) // you are not returning anything, so use void.
{
    newNode=getmem();
    strncpy(newNode->words,text, 29); // you can't just assign character arrays. Use strncpy to copy the contents.
    newNode->next=head;
    head=newNode;
};

Also once you are finished with the program, free the memory used in the list, or you will be leaking memory.

thank you so much, it is now saving in the list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.