这里引用了一个新的库,叫#include “sstream”。
可以同时转换不同类型
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string s;
string answer = "";
//3,3,4,2,10,3,4,7
//3,3,4,2,10,3,4,7,5
/*
She was born in 1990-01-02 and from Beijing city.
*/
string int2str(int aNum)
{
stringstream res;
res << aNum;
string s;
res >> s;
return s;
}
int main()
{
getline(cin, s);
int length = s.size();
int sum = 0;
for(int i = 0; i < length; i++)
{
if(s[i] == ' '){
if(sum != 0){
answer += int2str(sum) + ",";
sum = 0;
}
else
sum = 0;
}
else
sum++;
}
answer += int2str(sum) + ",";
int ansl = answer.size();
for(int i = 0; i < ansl - 1; i++)
cout << answer[i];
cout << endl;
return 0;
}