#include <iostream>
#include <string>
#include <vector>
std::vector < std::string > MakeVector( std::string str );
int main( int argc, char* argv[] ) {
std::string str = "Hello, how are you?";
std::vector < std::string > vec = MakeVector( str );
for ( int i = 0; i < vec.size(); i++ ) {
std::cout << vec[ i ] << " ";
}
return 0;
}
std::vector < std::string > MakeVector( std::string str ) {
std::string temp;
std::vector < std::string > vec;
for ( int i = 0; i < str.size(); i++ ) {
if ( str[ i ] != ' ' ) {
temp += str[ i ];
}
elseif ( str[ i ] == ' ' ) {
vec.push_back( temp );
temp = "";
}
}
if ( vec.size() == 0 ) {
vec.push_back( str );
}
return vec;
}
It is supposed to divide the string in words, store each word in a vector and then iterate through the vector and output each word. The problem is, the output I get here is "Hello, how are".
Any help?
Thanks!