Description
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"
Analyse
将字符串全变成小写的,简单题
for循环字符串内每个字符,如果是大写字母则变成小写字母,通过ASCII的方式
A 65
Z 90
a 97
z 122
string toLowerCase(string str)
{
for (int i = 0; i < str.length(); i++)
{
if ( 'A' <= str[i] && str[i] <= 'Z')
{
str[i] = str[i] + ('a' - 'A');
//str[i] = str[i] + 32;
//str[i] = str[i] | 32; //32的二进制是0010 0000, str[i] | 32是把str[i]代表32的那个二进制位变成1,那个位如果原来为0和32与运算相当于+32
}
}
return str;
}