I must tokenize a string with this format:
pippo:c:C2,C3,C4:pippo
how do I tokenize this string using strtok (i don't need the string once i've done the tokening anymore). I want the lone character to be stored into a char variable and not in a char* variable.
If you only want the c in the :c: part, and you know that things will have this format, you don't have to go as far as tokenizing. You can simply do
char c = strchr(str, ':')[1];
This locates the first :, and gets the character following it. Of course, strtok would also work:
strtok(str, ":");
char c = strtok(NULL, ":")[0];
This fetches the second tokenized word, and get the first character of that word. In general, if you wanted things further in, you would probably want to use a loop instead. Not that the str buffer must be writeable in this case, unlike the others.
Finally, if the pippo part always has the same length, you can get away with simply
char c = str[6];
In all cases, str = "pippo:c:C2,C3,C4:pippo".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With