Search⌘ K
AI Features

Count Vowels in a String

Explore how to count the number of vowels in a string using both iterative and recursive methods in Python. This lesson helps you understand how to traverse strings, implement recursion, and compare these approaches, preparing you for coding interviews focused on recursion concepts.

Problem Statement

Imagine that we have to find the number of vowels in a given string. We know that the English alphabet contains 55 vowels: a,e,i,o,ua, e, i, o, u. Let’s solve this problem using both iterative and recursive methods.

Iterative Method

To solve this problem we have to traverse the entire string so that we can initiate a simple for loop.

Let’s take a look at the code:

Python 3.5
def isVowel(character): # function to check whether input character is a vowel
character = character.lower() # convert character to lower case so upper cases can also be handled
vowels = "aeiou" # string containing all vowels
if character in vowels: # check if given character is in vowels
return True
else:
return False
def countVowels(string): # function that returns the count of vowels
count = 0
for i in range(len(string)):
if isVowel(string[i]) : # check if character is vowel
count += 1
return count
# Driver code
string = "Educative"
print(countVowels(string))

In the code snippet above, the main function countVowels() takes a string as its input. It traverses the entire length of the string. The condition

i in range(len(string))

ensures that the for loop breaks if i becomes equal to the length of the string calculated using len(string). Next, we use a condition

isVowel(string[i])

to determine whether or not that particular character is a vowel. If this condition is satisfied, we increment count by 11.

isVowel() is a function that returns TRUE if the character it passes is a vowel. It returns FALSE if that character is not a vowel.

Recursive Method

Let’s take a look at the recursive counterpart:

The code will look something like this:

Python 3.5
def isVowel(character): # function to check whether input character is a vowel
character = character.lower() # convert character to lower case so upper cases can also be handled
vowels = "aeiou" # string containing all vowels
if character in vowels : # check if given character is in vowels
return True
else:
return False
def countVowels(string, n): # function that returns the count of vowels
# Base Case
if n == 0 :
return 0
if n == 1 :
return isVowel(string[0])
# Recursive Case
return countVowels(string, n - 1) + isVowel(string[n - 1])
# Driver code
string = "Educative"
print(countVowels(string, len(string)))

The isVowel() function checks whether or not the character being input to it is a vowel. The major change that occurs is the conversion of the for loop into the recursive call.

Each time, the length of the input string is reduced in the iterative method. This is also done in the recursive method. We reduce the length of the input string and pass another call of the same function. The leftover letter is examined to check if it is a vowel. The result is added in the function calls.


In the next lesson, we have a challenge for you to solve.