Skip to content

Latest commit

 

History

History
56 lines (41 loc) · 1.23 KB

709. To Lower Case.md

File metadata and controls

56 lines (41 loc) · 1.23 KB

1. Description

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"

2. Solutions

Solution 1: Language: C Magic Number, 'a' - 'A'

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;
}

3. Testing Errors

  • Should not use str[n] += 32 because the machine we used might be a non-ASCII interchange one.