709. To Lower Case

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"

题目描述:实现一个 ToLowerCase 函数,函数功能是将字符串中的大写字母变成小写字母。

题目分析:很简单,我们可以利用 ASCII 的性质, A-ZASCII 的范围是 65~90 ,所以我们只需要将字符串中出现如上字母加上32即可。

python 代码:

class Solution(object):
    def toLowerCase(self, str):
        """
        :type str: str
        :rtype: str
        """
        str_length = len(str)
        s = ''
        for i in range(str_length):
            if(ord(str[i]) >= 65 and ord(str[i]) <= 90):
                s = s + chr(ord(str[i]) + 32)
            else:
                s = s + str[i]
                
        return s

C++ 代码:

class Solution {
public:
    string toLowerCase(string str) {
        int len = str.length();
        for(int i = 0; i < len; i++){
            if(str[i] >= 'A' && str[i] <= 'Z'){
                str[i] += 32;
            }
        }
        return str;
    }
};

相关文章:

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