I'm currently writing a task for uni trying read in a data from either a file or stdin, tokenising allt the words and presenting a list of occurring words and the count of each word.
Example of desired output:
Good bye occurred 5 times
This occurred 3 times
Hello occurred 1 time
Example of current output:
This
1
is
1
fun!
1
This
1
is
1
fun!
Don't mind the formatting of the output. That is an issue to be fixed later.
I have a running program that uses a linked list declared as follows:
typedef struct node
{
char word[50];
int count;
struct node * next;
}words;
The linked list is initialised as follows
words * head = NULL;
head = malloc(sizeof(words));
And two pointers assigned to the list
words * current = head;
words * search = head;
What I'm struggling with is the following piece of code:
while (!feof(input_file))
{
while(current->next != NULL)
{
current = current-> next;
}
//Test-loop for tokenisation
while(fgets(buffer,512, input_file) != NULL)
{
//Declaration of char token
char* token;
//Declaration of flag
int duplicate_word = 1;
//Test for-loop
for (token = strtok(buffer, " "); token != NULL; token = strtok(NULL, " "))
{
char duplication_check_token[60];
strcpy(duplication_check_token, token);
while(search != NULL)
{
char duplication_check_search[60];
strcpy(duplication_check_search, current -> word);
if (strcmp(duplication_check_search, duplication_check_token) == 0)
{
search->count++;
duplicate_word = 0;
break;
}
search = search -> next;
}
if (duplicate_word != 0)
{
while(current->next != NULL)
{
current = current-> next;
}
current = malloc(sizeof(words));
strcpy(current -> word, token);
current -> count = 1;
current -> next = NULL;
//Test print
printf("%s\n", token);
printf("%d\n", current -> count);
}
}
}
When debugging, it never seems to check through the entire linked list in the while(search != NULL)
loop.
What part of the logic am I getting wrong?
Thank you for any help!