There are some things you must change:
char *cstr1 = new char[s1.length()];
c-string are null-terminated, so you need one more char to store the null character:
char *cstr1 = new char[s1.length() + 1];
(same for
cstr2
)strCheck1.append(p1)
p1
cannot be a null pointer (see Assign a nullptr to a std::string is safe? for further details). So you have to check...if (p1) strCheck1.append(p1);
(same for
p2
).cout << p1 << endl
if
p1
is a null pointer bad things can happen (see Why does std::cout output disappear completely after NULL is sent to it). So you have to check...if (p1) { cout << "p1 " << p1 << endl; strCheck1.append(p1); }
(same for
p2
)there is a memory leak (
cstr1
/cstr2
must be deleted).
At the end it should work.
Probably you should consider other systems to extract tokens (where you haven't to mix std::string and c-string). E.g.:
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string text("text-to-tokenize");
std::istringstream iss(text);
std::string token;
while(getline(iss, token, '-'))
std::cout << token << std::endl;
return 0;
}