Open In App

Python Program to Count Vowels in a String Using Recursion

Last Updated : 07 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string, our task is to count the number of vowels in the string using recursion in Python and return the number of vowels.

Examples: 

Input: acbed
Output: 2
Input: geeksforgeeks
Output: 5
Explanation: We are counting vowels in a string and printing the number of vowels.

Python Program to Count Vowels in a String Using Recursion

We can use a recursive approach to count vowels in a string by breaking the string into smaller parts and recursively counting the vowels in each substring. The base case is when the string becomes empty, at which point we return 0. Otherwise, we check if the first character is a vowel and add 1 to the count if true. We then recursively call the function with the remaining substring.

Algorithm

  1. Define a recursive function that takes a string as input.
  2. If string is empty, return 0.
  3. If the first character of string is a vowel:
    • Increment the count by 1.
    • Recursively call the substring starting from index 1.
  4. If the first character is not a vowel:
    • Recursively call the substring starting from index 1.
  5. Return the total count.

Example 1: Count Vowels in a Python String Using Recursion

In this example, the Python function count_vowels recursively calculates the number of vowels in a given string, considering both uppercase and lowercase vowels. The function returns the total count, and the provided usage demonstrates counting vowels in the string “Geeksforgeeks,” printing the result.

Python3




def count_vowels(string):
    # Base case: if the string is empty, return 0
    if not string:
        return 0
    # Check if the first character is a vowel
    if string[0].lower() in 'aeiou':
        return 1 + count_vowels(string[1:])
    else:
        return count_vowels(string[1:])
 
 
# Example usage:
string = "Geeksforgeeks"
print("Number of vowels:", count_vowels(string))


Output

Number of vowels: 5


Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(n), where n is the length of the string since the function is calling itself n times.

Example 2: Count Vowels in a Long Sentence Using Recursion in Python

n this example, the Python function count_vowels recursively calculates the number of vowels in a given string, treating both uppercase and lowercase vowels equally.

Python3




def count_vowels(string):
    # Base case: if the string is empty, return 0
    if not string:
        return 0
    # Check if the first character is a vowel
    if string[0].lower() in 'aeiou':
        # If it is, add 1 and recurse on the rest of the string
        return 1 + count_vowels(string[1:])
    else:
        # If it's not a vowel, just recurse on the rest of the string
        return count_vowels(string[1:])
 
 
# Example usage:
string = "Hello World, This is GeeksforGeeks World!"
print("Number of vowels:", count_vowels(string))


Output

Number of vowels: 11


Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(n), where n is the length of the string since the function is calling itself n times.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads