strtok works by replacing references to the token with a NULL pointer so that the string argument is itself modified. Often this is an undesirable consequence.
A simple solution is to simply copy the string and input the copy to strtok. However, this does seem inefficient and a little inelegant.
So I was wondering whether it is possible to create a function that is effectively strtok without modifying the string? I assume not as otherwise strtok would have done so!
sure -- you can use strstr to find the hits without modification, and do what you will with the answers.
all of which you can do with std::string. I don't know if it has a direct strtok-like algorithm or if you would have to cook it up with find and substring functions but it should be straightforward.
Do you need a real time solution or a C solution? What is your goal / need here?
A simple solution is to simply copy the string and input the copy to strtok. However, this does seem inefficient and a little inelegant.
Your solution makes copies on the characters in the string too. So it's (approximiately) equally inefficient. If you must use C-strings and your own memory-management then strdup() & strtok() (or strtok_r() for thread safety if available/necessary) seem fine to me.