Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Uppercase Conversion Recursively

Convert text to uppercase through recursive calls. Students transform one indexed character at a time and concatenate returned text without modifying the string.

AdvancedPhase 3 - Level 4: String-based RecursionExample 8 of 10
uppercase-conversion-recursively.py
1def to_upper(s, index=0):
2 # Base case
3 if index >= len(s):
4 return ""
5
6 # Convert current character
7 char = s[index].upper()
8 # Recurse
9 return char + to_upper(s, index + 1)
10
11# Test
12text = input("Enter a string: ")
13result = to_upper(text)
14print(f"Uppercase: {result}")

Output

Enter a string: Hello
Uppercase: HELLO

In this one

  • Transform one character at a time
  • Return an empty tail
  • Understand Unicode expansion

How it works

  1. 1

    Stop at the length boundary with empty text.

  2. 2

    Uppercase the current character.

  3. 3

    Concatenate it with the recursively converted remainder.

Edge cases

What does empty input produce?

What does empty input produce?

An empty result.

Must output length equal input length?

Must output length equal input length?

No. Some Unicode uppercase mappings expand characters.

What's going on

At each position, upper produces that character's uppercase text. The function joins it to the processed result from the next index.

The base case returns an empty tail. Digits and punctuation generally remain unchanged. Unicode case mapping can expand a character into several characters, so output length need not equal input length. Calls and concatenations still require extra memory.

What does empty input produce? An empty result.

Must output length equal input length? No. Some Unicode uppercase mappings expand characters.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What advances the recursion?

Pick an answer to check it.

Question 2

What does empty input produce?

Pick an answer to check it.

Questions people ask

What does empty input produce?

An empty result.

Must output length equal input length?

No. Some Unicode uppercase mappings expand characters.

What advances the recursion?

index + 1.