I use
// str is a string that holds the line of data from ifs- the text file. // str holds the words to be split, res the vector to store them in. while( getline( ifs, str ) ) split(str, res); void split(const string& str, vector<string>& vec) { typedef unsigned int uint; const string::size_type size(str.size()); uint start(0); uint range(0); /* Explanation: * Range - Length of the word to be extracted without spaces. * start - Start of next word. During initialization, starts at space 0. * * Runs until it encounters a ' ', then splits the string with a substr() function, * as well as making sure that all characters are lower-case (without wasting time * to check if they already are, as I feel a char-by-char check for upper-case takes * just as much time as lowering them all anyway. */ for( uint i(0); i < size; ++i ) { if( isspace(str[i]) ) { vec.push_back( toLower(str.substr(start, range + 1)) ); start = i + 1; range = 0; } else ++range; } vec.push_back( toLower(str.substr(start, range)) ); }
I'm not sure if this is especially useful for you, but I will try. The toLower function is a quick function that simply uses the :: toLower () function. This reads each char to a space, and then fills it with a vector. I'm not quite sure what you mean by char from char.
Do you want to display the word character at a time? Or do you want to test each character as you go? Or do you want you to extract one word, finish, and then come back? If so, I would 1) recommend the vector anyway, and 2) let me know so that I can refactor the code.
source share