#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;
}

line 42: cstr pointer has not been assigned any memory, and scanf() is just scribbling all over memory. So what are you attempting to do in lines 41-43 anyway? Since the text at line 40 does not contain any spaces then line 42 sscanf() would be the same as strcpy(), and consequently line 43 is just a copy of line 40. Delete lines 41 and 42, then change line 43 like this: string str = sentence; .

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.