Member-only story
Interview question: Count the numbers of vowels and consonants in a string of text
If you are intending to get a job as a Python developer then you will no doubt be asked a few coding questions to test your knowledge of the language. One question that is likely to be asked is to count the number of vowels and consonants in a string of text.
There are several answers to this question, but I developed an answer to the question, as evidenced by the code in the screenshot below.
The solution to this interview question, written in psuedocode is:-
- Define the string,s.
- Define the array, vowels, as a list of vowels in the alphabet.
- Defined the array, consonants, as a list of the consonants in the alphabet.
- Define the variables, vowel_count and consenant_count, and initialise them to 0.
- Create a for loop that will iterate through the length of the string, s. If the iteration is a vowel then increment vowel_count by 1. If the iteration is a consonant then increment consenant_count by 1.
I looked on the internet and I found other ways to count the vowels and consonants in a string, but I prefer the method that I have just presented about. Feel free to experiment with the code and look for an algorithm that you prefer.