c - what makes it not working while inserting new elements after tokenizing -


i started work on c general idea. created linked list structure, can insert new elements without problem. idea of list increments count when there same element inserted, read words file way.

typedef struct node {    char* word;    int count;    struct node *next;    struct node *head;    struct node *current; } node; 

then when try use list in code below adds words doesn't increments count. acts word new element (it increments when hard code else multiple times insertfirst("teststring"))

      char* pch;       pch = strtok(line," ,.-()\r\n\t");       while (pch != null)       {         printf("%s-",pch);         int = 0;         for(; pch[i]; i++){            pch[i] = tolower(pch[i]);         }         insertfirst(r,(char*) pch,1); // inserts doesn't increment count;         pch = strtok (null, " ,.-()\r\n\t");      } 

the code above reads file line line, removes signs, spaces, new lines etc. want put words in list "r". sure there not issue in insertfirst method because works without tokenize

//insert method-wrong 1 void insertfirst(node* r, char* word, int count){    if(find(r, word)){        struct node* temp = find(r,word);        temp->count += 1; //    }    else{       struct node *link = (struct node*) malloc(sizeof(struct node));       strcpy(&link->word, &word);       link->count = count;       link->next = r->head;       r->head = link;    } } 

thanks comments code below works charm

//working insert method void insertfirst(node* r, char* word, int count){    if(find(r, word)){        struct node* temp = find(r,word);        temp->count += 1;    }    else{       struct node *link = (struct node*) malloc(sizeof(struct node));       link->word = malloc(strlen(word)+1);       strcpy(link->word, word);       link->count = count;       link->next = r->head;       r->head = link;    } } 

that solution vadim_hr solved

//working insert method void insertfirst(node* r, char* word, int count){    if(find(r, word)){        struct node* temp = find(r,word);        temp->count += 1;    }    else{       struct node *link = (struct node*) malloc(sizeof(struct node));       link->word = malloc(strlen(word)+1);       strcpy(link->word, word);       link->count = count;       link->next = r->head;       r->head = link;    } } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -