#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <string>
#include <cstring>
using namespace std;
void string_split_0(string& str, char split_char)
{
char* cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());
char* p = std::strtok(cstr, " ");
while (p != 0)
{
std::cout << p << '\n';
p = strtok(NULL, " ");
}
delete[] cstr;
}
void string_split_1(string& str, char split_char)
{
char* cstr = new char[str.length() + 1];
strcpy_s(cstr, str.length() + 1, str.c_str());
rsize_t len = sizeof(cstr);
const char* delim = " ";
char* next_token;
char* p = strtok_s(cstr, delim, &next_token);
while (p != 0)
{
std::cout << p << '\n';
p = strtok_s(NULL, delim, &next_token);
}
delete[] cstr;
}
int main()
{
string str = "please split this sentence into tokens";
string_split_0(str, ' ');
std::cout << std::endl;
string_split_1(str, ' ');
}
|