#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> g_split_string(const std::string& str, const char c)
{
vector<string> v;
cout << str << endl;
string::const_iterator s = str.begin();
bool flag = true; // continuous c
while (true) {
string::const_iterator begin = s;
while (*s != c && s != str.end())
{
++s;
flag = false;
}
if (!flag) v.push_back(string(begin, s));
if (*s == c) { flag = true; }
if (s == str.end())
{
break;
}
if (++s == str.end())
{
break;
}
}
return v;
}
int main ()
{
string sentence ="Rudolph,is,12,years,old";
char *cstr ;
sscanf(sentence.data(), "%s ", cstr);
string str=cstr;
cout << str << endl;
vector <string> str1 = g_split_string(str, ',');
vector <string>::iterator iter;
for (iter = str1.begin(); iter != str1.end(); iter++)
cout << *iter << endl;
return 0;
}
coolerli 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.