To Lower Case
Problem
Return a copy of the string with every ASCII uppercase letter converted to lowercase.
s = 'Hello''hello'def to_lower(s):
return ''.join(chr(ord(c) + 32) if 'A' <= c <= 'Z' else c for c in s)
function toLowerCase(s) {
let out = '';
for (const c of s) out += (c >= 'A' && c <= 'Z') ? String.fromCharCode(c.charCodeAt(0) + 32) : c;
return out;
}
String toLowerCase(String s) {
char[] a = s.toCharArray();
for (int i = 0; i < a.length; i++) if (a[i] >= 'A' && a[i] <= 'Z') a[i] += 32;
return new String(a);
}
string toLowerCase(string s) {
for (char& c : s) if (c >= 'A' && c <= 'Z') c += 32;
return s;
}
Explanation
We need to lowercase a string without using a built-in lowercase function, so we lean on a neat property of the ASCII table: every uppercase letter sits exactly 32 positions before its lowercase twin.
For each character c we ask one question: is it an uppercase letter, i.e. between 'A' and 'Z'? If yes, we add 32 to its code (ord(c) + 32) to land on the lowercase version. If not — a digit, space, symbol, or already-lowercase letter — we leave it untouched.
This works because 'A' is code 65 and 'a' is code 97, and that same gap of 32 holds for the entire alphabet. So shifting by 32 reliably converts A→a, B→b, and so on.
We do this for every character and join the results into the answer, which is why the time grows linearly with the string length.
Example: "Hello". The H (code 72) is uppercase, so it becomes h (code 104). The e, l, l, o are already lowercase and stay the same, giving "hello".