Question

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"


Analyze

This question is so easy. There is a formula for converting the capital letter to the lowercase.

The capital letter ASCII code is between [65-90]. And the lowercase is between [97-122], and the a = A + 32. As a result, when we find a capital letter, then just str[i] + 32.


Solution

class Solution {
public:
    string toLowerCase(string str) {
      int len = str.size();
      for (int i = 0; i < len; ++i) {
        if (str[i] >=65 && str[i] <= 90) {
          str[i] += 32;
        }
      }
      return str;
    }
};
By guozetang            Updated: 2020-09-19 13:02:30

results matching ""

    No results matching ""