I’m using strtok to split a string into tokens. Does anyone know any function which actually counts the number of tokens?
I have a command string and I need to split it and pass the arguments to execve() .
Thanks!
Edit
execve takes arguments as char**, so I need to allocate an array of pointers. I don’t know how many to allocate without knowing how many tokens are there.
One approach would be to simply use
strtokwith a counter. However, that will modify the original string.Another approach is to use
strchrin a loop, like so:If you have multiple delimiters, use
strpbrk: