This is my CPP solution.
class Solution {
public:
int countSegments(string s) {
int cnt = 0;
vector<string> store;
string container;
for(int i=0;i<s.size();i++){
if(s[i]==' '){
if(container.size()!=0)
store.push_back(container);
container.clear();
}
else
container.push_back(s[i]);
}
if(container.size()!=0)
store.push_back(container);
return store.size();
}
};