Count Vowels Recursively
Count vowels in a string recursively.
AdvancedPhase 3 - Level 4: String-based RecursionExample 3 of 10
count-vowels-recursively.py
1def count_vowels(s, index=0):2 # Base case3 if index >= len(s):4 return 056 # Check current character7 vowels = "aeiouAEIOU"8 count = 09 if s[index] in vowels:10 count = 11112 # Recurse13 return count + count_vowels(s, index + 1)1415# Test16text = input("Enter a string: ")17result = count_vowels(text)18print(f"Number of vowels: {result}")
Output
Enter a string: Hello Number of vowels: 2
What's going on
Check current character, recurse on rest.
Key Concepts:
Base case: index >= len(s)
Check if current char is vowel
Add 1 if vowel, recurse with index+1
🔗Related Content
- 📖
Phase 3 - Learn Concepts
Review Phase 3 concepts and explanations
- 📝
Phase 3 - Quiz
Test your Phase 3 understanding with quiz questions
- 💻
Phase 3 - All Practice Problems
Explore all practice problems for Phase 3
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum