I want to extract words from string.
I dont want to use strtok
because it will spoil my source string. Another thing is that I am wondering if it is possible to manage to do what I want without using cycles.
Here is my code sample. It successfully reads first word but second and third remain empty.
char source[] = "XXX|YYY|ZZZ";
char word1[10] = "";
char word2[10] = "";
char word3[10] = "";
sscanf( source, "%[^|]s|%[^|]s|%s", word1, word2, word3 );
Is it really possible to do it using sscanf
or I am on the wrong path?
UPDATE:
It looks like user3121023's answer does not work for empty words.
char source[] = "XXX||ZZZ";
char word1[10] = "";
char word2[10] = "";
char word3[10] = "";
sscanf( source, "%[^|]|%[^|]|%s", word1, word2, word3 );
Third word remains empty. What should I do in this situaltion?