I have a string of many letters
string path = "cxzaserds";
and a target word
string word = "cars";
In my function match()
, I want to return true if the characters from word
are found (in order) in path, in this case it would return true ('c' comes before 'a' comes before 'r' comes before 's' in the path
string).
I'm trying to use strtok()
to find each character one after another, with the delimiter being the current index's letter.
my progress:
bool match (string path, string word)
{
char * cstr = new char [path.length()+1]; //workaround for strtok on string
std::strcpy (cstr, path.c_str());
char *p;
for (int i = 0 ; i < path.length(); i++)
{
//error here, "invalid conversion from 'char' to 'const char*'
p = strtok (cstr, word[i]);
if (p != NULL) //if strtok found word[i]
continue;
else return false; //was NULL, word not found
}
return true; //made it through, return true
}
On the C++ page, under delimiters, it says:
These can be different from one call to another.
http://www.cplusplus.com/reference/cstring/strtok/
What can I do to change the delimiter as strtok returns non-null? Or another (easier) solution altogether?