Reverse String Words Recursively
Reverse order of words in string recursively.
AdvancedPhase 3 - Level 4: String-based RecursionExample 10 of 10
reverse-string-words-recursively.py
1def reverse_words(words, index=0):2 # Base case3 if index >= len(words):4 return ""56 # Recurse first7 result = reverse_words(words, index + 1)8 # Add current word9 if result:10 return result + " " + words[index]11 else:12 return words[index]1314# Test15text = input("Enter a string: ")16words = text.split()17result = reverse_words(words)18print(f"Reversed words: {result}")
Output
Enter a string: Hello World Programming Reversed words: Programming World Hello
What's going on
Recurse first, then add current word.
Key Concepts:
Split into words
Recurse before adding word
Creates reverse order
🔗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