Case Changer You are given a string T. Your task is to modify the string based on the…
- User
Case Changer
You are given a string T. Your task is to modify the string based on the following rule:
• If the first character of the string is an uppercase letter, convert all subsequent characters to uppercase.
• If the first character is a lowercase letter, convert all subsequent characters to lowercase.
Your job is to correct the string according to these rules and return the modified string.
Input Format
• The input contains the string representing
T
Output Format
• Print the modified string.
Constraints
• 0 <=length(T) <= 105
Sample Input 1
Hello World
Sample Output 1
HELLO WORLD
Sample Input 2
HELLO WORLD
Sample Output 2
hello world
- Assistant
To achieve the task of modifying the string according to the given rules, we will follow these steps:
- Check if the string is empty. If it is, return it as is.
- Examine the first character of the string:
- If it is an uppercase letter, convert all subsequent characters to uppercase.
- If it is a lowercase letter, convert all subsequent characters to lowercase.
- Print the modified string.
Here is the Python code to implement the solution:
def case_changer(T): if not T: return T if T[0].isupper(): return T.upper() else: return T.lower() # Sample Input 1 T1 = "Hello World" print(case_changer(T1)) # Output: HELLO WORLD # Sample Input 2 T2 = "HELLO WORLD" print(case_changer(T2)) # Output: hello worldExplanation:
- The
if not T:check handles the edge case where the stringTis empty. T[0].isupper()checks if the first character is an uppercase letter.- Depending on whether the first character is uppercase or lowercase, we use
T.upper()orT.lower()to convert the entire string accordingly and return the modified string.
This ensures that our solution adheres to the specified rules and constraints.