To Lower Case
You get a string s. Return a new string in which every uppercase English letter, A to Z, is replaced by its lowercase letter. Every other character, such as a lowercase letter, a digit or a symbol, stays exactly as it is.
Function
- sstring
- the text to convert
- Returnsstring
- s with every uppercase letter made lowercase
Constraints
1 ≤ s.length ≤ 104- Every character of
sis an English letter, a digit, or one of the symbols!#$%&()*+-./:;<=>?@[]^_`{|}~.
Examples
- Input
- s = "Hello-World!"
- Output
- "hello-world!"
- Explanation
- The capitals
HandWbecomehandw. The-and the!are not letters, so they stay where they are.
- Input
- s = "coddy_2026"
- Output
- "coddy_2026"
- Explanation
- There is no uppercase letter to change. The lowercase letters, the
_and the digits come back unchanged.
- Input
- s = "SQL"
- Output
- "sql"
- Explanation
- All three characters are capitals, so each one moves to its lowercase letter.
Follow-up
Can you write toUpperCase with the same loop, and explain why one bit of the character code is all that differs between A and a?
Hints
Open them one at a time. Each one gives away a little more.
A computer stores each character as a number. How do the numbers for
Aandarelate?AtoZare the codes 65 to 90, andatozare 97 to 122. Each capital is exactly 32 below its lowercase letter.Go through the characters one by one. When a code is between 65 and 90, add 32; otherwise keep the character. Collect the results and join them into the answer.
Solution
Every language ships a lowercase function, and in production code you should call it. Interviewers ask this problem to see whether you know what it does: in ASCII each capital letter sits exactly 32 codes below its lowercase letter, so you add 32 to every code between 'A' and 'Z'.
Call the built-in lowercase function
Intuition
Python's lower(), JavaScript's toLowerCase(), Ruby's downcase and their relatives already do this job; C and C++ offer tolower for one character, so you call it in a loop. Each character is looked at once, O(n) time, and the new string takes O(n) space.
Some of these functions follow the machine's language settings. In Turkish the lowercase of I is a dotless ı, so the Java and C# versions ask for a fixed rule with Locale.ROOT and ToLowerInvariant. In an interview this answer is often not accepted, because it hides the idea the question is about.
Algorithm
- Call your language's lowercase function on
s, or calltoloweron each character in C and C++. - Ask for a fixed, language independent rule where the function offers one.
- Return the result.
def toLowerCase(s):
return s.lower()Shift the character codes of capital letters
Intuition
Every character is stored as a number. In ASCII, A to Z are the codes 65 to 90 and a to z are 97 to 122. Both runs are in alphabetical order and start 32 apart, so the lowercase of any capital is its code plus 32: H is 72 and h is 104. Only codes 65 to 90 change; symbols such as _ sit between Z and a and must be copied as they are.
Each character is checked once, O(n) time, and the answer is a new string of the same length, O(n) space. Writing 'a' - 'A' instead of 32 shows where the number comes from.
Algorithm
- Copy
sinto an array of characters or codes. - For each position, read the character code.
- If the code is between
'A'(65) and'Z'(90), add 32. - Turn the array back into a string and return it.
def toLowerCase(s):
# Every capital letter sits 32 codes below its small letter: 'A' is 65, 'a' is 97.
shift = ord("a") - ord("A")
chars = []
for ch in s:
if "A" <= ch <= "Z":
ch = chr(ord(ch) + shift)
chars.append(ch)
return "".join(chars)
Pitfalls and edge cases
- Adding 32 to every character that is not lowercase. Digits and symbols change too:
1becomesQ. - Checking only one end of the range.
code < 'a'also catches[,_and@, andcode >= 'A'also catches every lowercase letter. - Using
<instead of<=at the ends, which leavesAorZuppercase. - Building the answer with
result = result + chon an immutable string. Each step copies everything so far, which is quadratic forn = 10^4. - In C, writing into the input or forgetting the terminating
'\0'. Allocaten + 1bytes for the copy.
Frequently asked questions3
Why is the difference between uppercase and lowercase 32?
ASCII places the capitals at codes 65 to 90 and the small letters at 97 to 122, with six symbols in between. Both alphabets are in the same order, so each pair is 97 - 65 = 32 apart. 32 is a single bit, so setting that bit turns a capital into its lowercase letter.
Can you change the case with a bit operation?
Yes. For a capital letter, code | 32 sets the bit that separates the two cases and gives the lowercase letter, and code & ~32 clears it again. You still need the range check first, because the same bit trick would also change digits and symbols.
What is the time complexity of converting a string to lowercase?
It is O(n) for a string of length n, because each character is checked once. The new string takes O(n) space. If you can change a character array in place, the extra space drops to O(1).
Similar problems
Problems that use the same ideas. Solving two or three of them is what makes a pattern stick.
def toLowerCase(s):
# Write code hereTest #1
Test #2
Test #3
Argument
s = "Hello-World!"
Expected Output
"hello-world!"