I'm trying to figure out how to extract a word, defined as a sequence of letters, from a string. I'm fairly new to C and therefore am encountering quite a bit of confusion with pointers.
An example of what I am trying to do: If the string is: ??<Hello...how<are@@you<? I need to extract the words "Hello" "how" "are" and "you", and put the four of them into a linked list. So only letters. Also, whitespace is defined as any character for which isspace() returns a non-zero value.
A word is at most 64 bytes long.
I'm using a linked list and a function read_words(char *s) that takes in the string read in from a file in main using fscanf.
Basically, I want to read in all the strings, and each one read in call read_words() to get the words out of it and into the linked list for further use.
Here is the code I've written, that seems to be throwing an error to do with the pointers:
struct node { char *val; struct node *next; int count; } *words = NULL; void read_words(char *s) { struct node *tmp; char word[64+1]; int i, check, wordStarted = 0, count = 0; for (i = 0; s[i] != '\0'; i++) { if ((isspace(s[i]) != 0) || !isalpha(s[i])) { if (wordStarted == 1) { check = check_list(word); if (check != 1) { tmp = malloc(sizeof(struct node)); tmp->val = word; tmp->count = 1; tmp->next = words; words = tmp; } count = 0; wordStarted = 0; } } else { word[count++] = s[i]; wordStarted = 1; } } }
Any help with this would be greatly appreciated!
Thank you!