Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
Example 1:
Input: "Hello"
Output: "hello"
Example 2:
Input: "here"
Output: "here"
Example 3:
Input: "LOVELY"
Output: "lovely"
To converse upper case to lower case, we need to add 32, which is the difference between 'a'
and 'A'
. However, for a non-ASCII machine, 'a' - 'A'
might not equal to 32.
- Wednesday, 26 August, 2020
- Time Complexity:
$O(n)$ - Space Complexity:
$O(1)$ - Runtime: 0 ms, faster than 100.00% of C online submissions for To Lower Case.
- Memory Usage: 5.5 MB, less than 23.36% of C online submissions for To Lower Case.
// Eg: In ASCII table, char 'a' = 97, and char 'A' = 65.
char *toLowerCase(char *str) {
int n = 0;
while (str[n] != '\0') {
if (str[n] >= 'A' && str[n] <= 'Z') {
str[n] += TOLOWER;
}
n++;
}
return str;
}
- Should not use
str[n] += 32
because the machine we used might be a non-ASCII interchange one.