Logic Building
def remove_char(s, char_to_remove):
# Base case
if len(s) == 0:
return ""
# Check first character
if s[0] == char_to_remove:
return remove_char(s[1:], char_to_remove)
else:
return s[0] + remove_char(s[1:], char_to_remove)
# Test
text = input("Enter a string: ")
char = input("Enter character to remove: ")
result = remove_char(text, char)
print(f"Result: {result}")Output
Enter a string: Hello Enter character to remove: l Result: Heo
Skip character if matches, include otherwise.
Key Concepts:
- Base case: empty string
- If matches, skip and recurse
- Otherwise, include and recurse