I need to count the number of words and I am assuming the correct way to do it is by calculating the number of times that the previous character in a string is not a letter
This problem is slightly more complicated than your algorithm allows.
This looks like homework, so I don't want to provide any code. I suggest an alternative approach which is simpler to think about.