I'm really new to coding and I'm having some trouble with trying to split strings in C++. I'd like to know how to split a string, which is input as a const char names[] (i.e. "Mary, Jan, Jane")
in C++ without using any external libraries (i.e. I don't want to have to use #include <string>
etc. - although I can use #include <cstring>
).
I've tried using:
const char names[] = "Mary, Jan, Jane";
char *token = strtok(names, ",");
while (token != NULL) {
token = strtok(NULL, " ");
}
But I can't seem to pass in a const array of chars, and I'd also like to know then how you would access all the individual "tokens"?
ALSO I've tried changing the input to just char names[]
(but I do need the input to be const), and I get a segmentation error and I don't understand why.