This Python program reads a character from the user and toggles its case using the XOR (^) operator.
In ASCII, the difference between uppercase and lowercase letters is 32, so XOR with 32 flips the case.
def toggle_case(char: str) -> str:
if 'A' <= char <= 'Z' or 'a' <= char <= 'z':
return chr(ord(char) ^ 32)
return char
# Read character from user
char = input("Enter a character: ")
# Convert and display
converted = toggle_case(char)
print(f"Toggled character: {converted}")
Input: A
Output: a
Input: g
Output: G