char *split(string s)
{
char holder[100]; // Holder for string to char[]
char fin[100]; // Final array
char * pch;
strcpy(holder, s.c_str());
pch = strtok(holder, " ");
while (pch != NULL)
{
printf("=>%s\n", pch);
strcpy(fin, pch); // copy pch into fin array
pch = strtok(NULL, " ");
}
return fin;
}
Is my current code, and I use like:string cmd;
getline(cin,cmd);
cout << split(cmd)<<endl;
It spits out each thing i type on a new line, seperated by a space...
But when I cout it, its a jumble of junk :
Is there a better way to do this, and where if i did
split(cmd)[0] the first element would be the first word separated by a space OR comma.. (Or any other deliminator i want)
Thanks