#include <cstring>
#include <iostream>
int main() {
char input[100] = "A bird, came. down; the. walk";
char *token = std::strtok(input, " ,.;");
// string token = std::strtok(input, " ,.;");
while (token != NULL) {
std::cout << token << '\n';
token = std::strtok(NULL, " ,.;");
}
}
C++ STL
The first puts the results in a pre-constructed vector, the second returns a new vector.
#include <string>
#include <sstream>
#include <vector>
#include <iterator>
template<typename Out>
void split(const std::string &s, char delim, Out result) {
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim)) {
*(result++) = item;
}
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, std::back_inserter(elems));
return elems;
}
No comments:
Post a Comment