Phase 3 - Level 4: String-based Recursion
Apply recursion to string operations.
10 programs · open one, then the next
Start with Reverse String Recursively- 01Reverse String RecursivelyAdvancedReverse text with recursive Python slicing. Students combine a reversed suffix with its first character and trace empty and single-character stopping cases.Open example →pydef reverse_string(s): # Base case if len(s) <= 1: return s
- 02Check Palindrome String RecursivelyAdvancedCheck text palindromes recursively with case-insensitive outer comparisons. Students trim matching ends, stop on a mismatch, and retain spaces and punctuation.Open example →pydef is_palindrome(s): # Base case if len(s) <= 1: return True
- 03Count Vowels RecursivelyAdvancedCount English vowels recursively by advancing an index. Students use a string-length base case and combine one-character contributions into a returned count.Open example →pydef count_vowels(s, index=0): # Base case if index >= len(s): return 0
- 04Remove Spaces RecursivelyAdvancedRemove ordinary spaces recursively from text. Students choose whether to keep the first character and distinguish a literal space from every whitespace character.Open example →pydef remove_spaces(s): # Base case if len(s) == 0: return ""
- 05Replace Character RecursivelyAdvancedReplace matching characters through a recursive text scan. Students carry old and new values, advance an index, and rebuild a string without changing the source.Open example →pydef replace_char(s, old, new, index=0): # Base case if index >= len(s): return ""
- 06Remove Occurrences RecursivelyAdvancedDelete every occurrence of one character with recursion. Students compare the first character, skip matches, and rebuild the remaining text in original order.Open example →pydef remove_char(s, char_to_remove): # Base case if len(s) == 0: return ""
- 07Print Characters RecursivelyAdvancedPrint text one character per line with Python recursion. Students advance an index, keep the original string unchanged, and stop before indexing past the end.Open example →pydef print_chars(s, index=0): # Base case if index >= len(s): return
- 08Uppercase Conversion RecursivelyAdvancedConvert text to uppercase through recursive calls. Students transform one indexed character at a time and concatenate returned text without modifying the string.Open example →pydef to_upper(s, index=0): # Base case if index >= len(s): return ""
- 09Count Vowels & Consonants RecursivelyAdvancedCount vowels and consonants recursively with carried totals. Students filter letters, lowercase each character, and return two values at the stopping index.Open example →pydef count_vc(s, index=0, vowels=0, consonants=0): # Base case if index >= len(s): return vowels, consonants
- 10Reverse String Words RecursivelyAdvancedReverse word order with a recursive list helper. Students split whitespace, build the suffix result first, and join words without reversing letters inside them.Open example →pydef reverse_words(words, index=0): # Base case if index >= len(words): return ""