Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.

 

Example 1:

Input: 

Example 2:

Input: 

Example 3:

Input: 

 

这道题让我们将单词转为小写,是一道比较简单的题目,我们都知道小写字母比其对应的大写字母的ASCII码大32,所以我们只需要遍历字符串,对于所有的大写字母,统统加上32即可,参见代码如下:

 

class Solution {
public:
    string toLowerCase(string str) {
        for (char &c : str) {
            if (c >= 'A' && c <= 'Z') c += 32;
        }
        return str;
    }
};

 

参考资料:

https://leetcode.com/problems/to-lower-case/

 

LeetCode All in One 题目讲解汇总(持续更新中...)

相关文章:

  • 2022-12-23
  • 2021-10-08
  • 2021-09-09
  • 2022-01-28
  • 2022-12-23
  • 2022-12-23
  • 2022-01-19
  • 2022-12-23
猜你喜欢
  • 2021-12-08
  • 2022-01-15
  • 2021-11-22
  • 2021-10-23
  • 2021-10-10
  • 2022-12-23
  • 2021-12-19
相关资源
相似解决方案