Reputation: 103
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;
}
}
Upvotes: 0
Views: 120
Reputation: 24921
The line
scanf("%30s", input);
which you are using will only read a single word of input.
The line
fgets( input, sizeof input, stdin );
will read an entire line of input, including the '\n'
character.
Therefore, I suggest that you use fgets
instead, but remove the trailing newline character, as described here:
Removing trailing newline character from fgets() input
The easiest way to do this is probably like this:
//attempt to read one line of input
if ( fgets( input, sizeof input, stdin ) == NULL )
{
fprintf( stderr, "input error!\n" );
exit( EXIT_FAILURE );
}
//remove newline character, if it exists
input[strcspn(input,"\n")] = '\0';
Upvotes: 2