742. To Lower Case
Difficulty: Easy
Topics: String
Similar Questions:
Problem:
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"
Solutions:
class Solution {
public:
string toLowerCase(string str) {
for (int i = 0; i < str.length(); ++i) {
if (isUpperCase(str[i])) {
str[i] += 'a' - 'A';
}
}
return str;
}
private:
inline bool isUpperCase(char c) {
return c >= 'A' && c <= 'Z';
}
};