I am havin this issue where if I input a string of words separated by commas with no leading nor
trailing spaces, I got my expected outputs. But, when I add spaces I just get one of the outputs without the spaces even though I don't have code that does that. It seems likes strtok
only reads the first one when there are spaces, even though there are more.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define SIZE 40
void main() {
char input[SIZE];
printf("Enter list of words separated by commas: ");
scanf("%30s", input);
char *arr = strtok(input, ",");
word *head, *current, *next;
current = (word*) malloc(sizeof(word));
assert(current != NULL);
head = current;
while (arr != NULL) {
next = (word*) malloc(sizeof(word));
assert(next != NULL);
// deleteWhiteSpaces(arr);
strcpy(current->value, arr);
arr = strtok(NULL, ",");
if (arr != NULL) {
current->next = next;
current = next;
}
}
current = head;
while (current) {
printf("'%s' %p\n", current->value, current);
current = current->next;
}
}