After reading "hey"
and "whats"
, the value of i
is more than the length of "hello"
and hence no such substring exists for the code input.substr(lastFind, i)
.
You should check for the length of possible substring (dictionary[j]
) and not i
.
input.substr( lastFind, dictionary[j].size() )
Also you will have to change:
lastFind += dictionary[j].size();
So the if loop becomes:
if(dictionary[j] == input.substr(lastFind, dictionary[j].size() ))
{
lastFind += dictionary[j].size();
output += dictionary[j] + " ";
}