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
if (string.charAt(i-1) == alphabets.charAt(j)) { counter++; }
You are incrementing the counter if the character is some alphabet character. You should increment it if it is no alphabet character.