C/C++ code#include
#include
using namespace std;
int main() {
cout << "Plz input a string:\n";
string strname ;
getline(cin, strname);
cout << strname << endl;
int i = 0, j = 0, k = 1; //k为将要分得的string的个数
string::iterator p = strname.begin();
while (p != strname.end()) {
if (*p++ == ',') {
k++;
}
}
int douhao = 0;
int *count = new int[k + 1]; //count数组计算两端以及逗号的位置
count[0] = -1;
count[k] = (strname.end() - strname.begin() );
p = strname.begin();
while (p != strname.end()) {
if (*p == ',') {
douhao++;
count[douhao] = (p - strname.begin());
}
p++;
}
string *mystr = new string[k];
p = strname.begin();
cout << "I get:\n";
for (int i = 0; i < k; i++) {
mystr[i] = string(p + 1 + count[i], p + count[i+1]); //取得分开后的字符串
cout << mystr[i] <
}
delete []count;
delete []mystr;
}