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.
+15 hidden tests on Submit
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 that function does: a character is a number, and in ASCII each capital letter sits exactly 32 codes below its lowercase letter. Check whether a code falls between 'A' and 'Z', and add 32 if it does.
Call the built-in lowercase function
Intuition
Python's lower(), JavaScript's toLowerCase(), Ruby's downcase and their relatives in every other language 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, so the work is O(n), and the new string takes O(n) space.
Some of these functions follow the language settings of the machine. In Turkish, the lowercase of I is a dotless ı, not i. That is why the Java and C# versions ask for a fixed rule with Locale.ROOT and ToLowerInvariant, so the answer does not change from one computer to another.
This is the right answer at work. In an interview it is often not accepted, because it hides the one 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.
So walk the string once. If a code lies between 65 and 90, add 32; otherwise copy the character as it is. The range check matters: the symbols [, ^ and _ sit between Z and a, and @ sits right before A. Testing only code < 97 would turn _ into a different symbol.
Each character is checked once, which is O(n) time. The answer is a new string of the same length, O(n) space. Writing 'a' - 'A' instead of 32 says the same thing and explains 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
The loop is short, so the mistakes are in the range check and in how the answer is built.
- Adding 32 to every character that is not lowercase. Digits and symbols would change too:
1would becomeQ. - 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 questions4
How do you convert a string to lowercase without a built-in function?
Loop over the characters and look at each character code. If the code is between 65 (A) and 90 (Z), add 32 to get the lowercase letter; leave every other character alone. Join the characters back into a string.
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.
Python
def toLowerCase(s):
# Write code hereCase 1
Case 2
Case 3
Input
s = "Hello-World!"
Expected
"hello-world!"