I'm trying to write the unique strings in a file to a linked list and increment a count for each duplicate word. I want to use a getNextWord function that returns a pointer to the next word in the file. The problem is that I am very new to c and pointers so I actually don't know what to do in my main method to call getNextWord and how to use this string pointer to actually get access to the string it's pointing at. So how do I use my function to get the string that needs to be a key for a node? Also any other advice would be greatly appreciated and if you see anything wrong with my function or struct please let me know! Thank you very much for your time. Here's my function and struct..
#define MAX_WORD_LEN 256
struct list {
int count;
char string[MAX_WORD_LEN];
struct list *next;
};
char* getNextWord(FILE* fd) {
char c;
char wordBuffer[MAX_WORD_LEN];
int putChar = 0;
while((c = fgetc(fd)) != EOF) {
if(isalnum(c)) break;
}
if (c == EOF) return NULL;
wordBuffer[putChar++] = tolower(c);
while((c = fgetc(fd)) != EOF) {
if(isspace(c) || putChar >= MAX_WORD_LEN -1) break;
if(isalnum(c)) {
wordBuffer[putChar++] = tolower(c);
}
}
wordBuffer[putChar] = '\0';
return strdup(wordBuffer);
}