LeetCode-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"

题目:将字符串中的大写字母转换成小写字母

思路:观察ASCII表,如果是大写字母,转换成小写字母就是将其ASCII值加32即可。

代码:

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

猜你喜欢

转载自blog.csdn.net/qq_29303759/article/details/81333787
今日推荐