Array from sentence

How do I make this work correctly for the photon? I’ve never used strtok before so I’m not sure. It keeps getting suggested in this forum. For right now it just displays “abc”. I’m expecting “xyz”.

char sentence[] =  "abc 123 xyz";
char *array[3];

array[0] = strtok(sentence, " ");
array[1] = strtok(sentence, " ");
array[2] = strtok(sentence, " ");

textToDisplay = String(sentence[2]);

With your code you are always starting the tokenisation from the beginning of the string.
You’d rather do it like this

array[0] = strtok(sentence, " ");
array[1] = strtok(NULL, " ");
array[2] = strtok(NULL, " ");

And here you can find the description of strtok()

Alternatively you can also use sscanf()

We also recommend to not use String wherever possible.


Thanks this works as expected!!

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.