To count words, we generate an array with the split() function and use the size of the generated array. To avoid extra empty words at start and end of the array, we remove all leading and trailing spaces.