I have implemented strtok and strtok_r with much ease recently. But I have encountered a strange problem. My sentence is something like this without the quotes: "now- the-boy is- going". The program works but only prints the word "now". I have noticed that if I close the spaces the entire sentence prints which I understand to be from the condition "while var!=NULL". But I need the spaces to remain so that at the end the program prints:"now the boy is going". How do I deal with this.
You now that the strtok() and strtok_r() functions are already present in arduino?
If you want help with your code you will have to post it (in code brackets)
Keep in mind that strtok()overwrites the delimiter with the null termination character when it finds a match. It then increments the internal pointer so it looks past the null on the next iteration. This is why only the first call passes the starting address of the object being searched and all others are null.
Apologies for turning up late. OK here is my code below. buf receives the chinese strings with dash delimiters and spaces. After the dash delimiter has been stripped, the chinese character is compared to the chinese array. if a match is found it simply prints out the english equivalent since the english array is the same size with the chinese array. I have tried to change the code below in different ways but I cant seem to figure out how to go about it.
char english[] = {'A','B'.......'Z'};
char chinese[] ={'x','xi'.......'yu'};// just an example
p = strtok(buf, "-"); //buf contains chinese string with dashes.
for (int i = 0; i < sizeof(chinese)/sizeof(chinese);i++){
while(p!=NULL){
if(strcmp(p,chinese[i]))
Serial.print(english[i]);
p = strtok(NULL, "-");
}
}[code]
Thank you UKhelibob. Those are just typo errors I did when i posted my response, sorry about that. @KeithRB i disagree with you somewhat. the xi prints out just fine if its put as the first character.
Once again the code I have posted only prints a single character. It only prints everything if i close the spaces. It seems something wrong happens if it encounters a space.
hose are just typo errors I did when i posted my response,
Why are you typing any code in here ? What is the point of posting code that is not the same as what you have in the IDE ? If you are going to type it in here then all bets are off as to what you are actually trying to compile and run.