Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Recursion Functions

Recursive function to count total vowels in a sentence - Python Program

Example 1 :

def count_vowels(s): """Count vowels recursively.""" if s == "": return 0 return (1 if s[0].lower() in "aeiou" else 0) + count_vowels(s[1:]) print(count_vowels("Recursion is powerful"))

Output

 
OUTPUT  :
8

Example 2 :

def isVowel(ch): return ch.upper() in ['A', 'E', 'I', 'O', 'U'] def countVowels(str, n): if (n == 1): return isVowel(str[n - 1]); return (countVowels(str, n - 1) + isVowel(str[n - 1])) str = input("Enter a String : ") print(countVowels(str, len(str)))

Output

 
OUTPUT  :
Enter a String  : Advanced Python Programming
7