c++

c++ tolower在循环中怎样用

小樊
82
2024-06-24 16:30:33
栏目: 编程语言

在C++中,我们可以使用std::tolower()函数将字符转换为小写形式。要在循环中使用std::tolower()函数,我们可以使用一个循环来遍历字符串的每个字符,并将每个字符转换为小写形式。

以下是一个示例循环,使用std::tolower()函数将字符串中的所有字符转换为小写形式:

#include <iostream>
#include <cctype>

int main() {
    std::string str = "Hello World";

    for (char &c : str) {
        c = std::tolower(c);
    }

    std::cout << "Lowercase string: " << str << std::endl;

    return 0;
}

在上面的示例中,我们使用for循环遍历字符串str中的每个字符,并使用std::tolower()函数将字符转换为小写形式。最后,我们打印出转换后的字符串。

0
看了该问题的人还看了