I just try to test how to split word in C++ but I faced with very unknown error. This problem really drives me insane, I don't know why it happens.
This is my code:
std::string key = "hello world";
std::string word = "";
for (int i = 0; i < (int)key.length(); i++)
{
if (std::isspace(key[i]) || key[i] == '\0')
{
std::cout << word << "\n";
word.clear();
}
else
word += key[i];
}
The result is just only "Hello", I tried to debug and figure out why the word stop concating after the isspace
condition? So could anyone please point me the correct direction?
Thanks
Edited: Tried the following, and now its missing the letter d in the end?
if (std::isspace(key[i]) || key[i + 1] == '\0')
{
Edit 2: Solved with this:
if (std::isspace(key[i]) || key[i + 1] == '\0')
{
if (key[i + 1] == '\0')
word += key[i];