Hi there, I was trying to make a chat bot, that learns from human and generate a sentece, etc, etc, so the first step is to transform a sentece in a dynamic array of words, and learn them. But I meet a problem
while (*current && *current == ' ')
{
current++;
}
// suppose current points to (a string that ends with) " ", where does current end up pointing to?
// if (*current) checks if the value of current is not zero (if it points to somewhere in memory).
// it does not check if current is pointing at your char array or even memory assigned to your program.
I would like to recommend you to stop using char pointers (or pointers to char pointers) and start using std::string as soon as possible. You will probably find that std::string has some more/safer features that will probably make your life easier.
Kind regards, Nico
Note that char pointers can also be very useful, if used with the right precautions (at least checking for '\0'). But usually if you can use a std::string: try those first.
A char array should always have a known length or (preferably) end with a null-termination ('\0'). If you don't use std::string you will have to make sure that this termination is indeed present.
If your char array is null-terminated or if you know the length of your char array you should make sure that you never iterate past the end of the char array (the easiest way is to check if the pointer currently points to '\0' before you iterate further, but you can also compare with the known length).
In any case it is your responsibility to make sure that your pointer points to somewhere inside your char array. You don't check for this, so your pointer will iterate out of the assigned memory and you end up with an access violation.
the first step is to transform a sentece in a dynamic array of words
so, it appears, that your basic objective is to split a string into its component words. There are several ways to do this: http://stackoverflow.com/questions/236129/split-a-string-in-c
also remember to remove any punctuation from the back of the individual words