Count Vowels & Consonants Recursively
Count vowels and consonants recursively.
AdvancedPhase 3 - Level 4: String-based RecursionExample 9 of 10
count-vowels-consonants-recursively.py
1def count_vc(s, index=0, vowels=0, consonants=0):2 # Base case3 if index >= len(s):4 return vowels, consonants56 # Check current character7 char = s[index].lower()8 if char.isalpha():9 if char in 'aeiou':10 vowels += 111 else:12 consonants += 11314 # Recurse15 return count_vc(s, index + 1, vowels, consonants)1617# Test18text = input("Enter a string: ")19v, c = count_vc(text)20print(f"Vowels: {v}, Consonants: {c}")
Output
Enter a string: Hello Vowels: 2, Consonants: 3
What's going on
Track vowels and consonants with parameters.
Key Concepts:
Use accumulator parameters
Check if character is vowel or consonant
Update counts and recurse
🔗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