Here is an example of counting words in a given text. I am using my own rules for determining what is a word and my definition of word might not match the generally accepted definition but you can change the logic in the code to make it meet your definition of word.