c++有没有字符串将所有字母转为小写的函数
时间: 2023-12-16 11:06:29 浏览: 33
是的,C++中有一个可以将字符串中的所有字母转换为小写的函数,它是`tolower()`函数。你可以使用该函数对字符串中的每个字符进行逐一转换。以下是一个示例代码:
```cpp
#include <iostream>
#include <cctype> // 包含tolower函数的头文件
int main() {
std::string str = "Hello World";
for (auto& c : str) {
c = std::tolower(c);
}
std::cout << str << std::endl; // 输出: hello world
return 0;
}
```
在上面的示例中,我们使用了一个循环来遍历字符串中的每个字符,并通过`std::tolower()`函数将字符转换为小写形式。最后,我们打印出转换后的字符串。
相关问题
C++将字符串中的小写字母转换成大写字母
可以使用C++的标准库函数toupper()将字符串中的小写字母转换成大写字母,具体的实现如下:
```c++
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
string str = "Hello, World!";
int len = str.length();
for(int i=0; i<len; i++)
{
if(islower(str[i])) // 判断是否为小写字母
{
str[i] = toupper(str[i]); // 转换成大写字母
}
}
cout << str << endl;
return 0;
}
```
在上述代码中,我们首先定义了一个字符串变量str,并且获取了其长度len。然后利用for循环遍历字符串中的每一个字符,判断其是否为小写字母,如果是,则调用toupper()函数将其转换成大写字母。最后输出转换后的字符串即可。
c++将字符串中的小写字母转换成大写字母
可以使用C++标准库中的toupper函数将小写字母转换成大写字母。
以下是一个例子:
```c++
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
char str[] = "Hello, World!";
int len = strlen(str);
for(int i = 0; i < len; i++) {
if(islower(str[i])) { // 判断是否为小写字母
str[i] = toupper(str[i]); // 转换为大写字母
}
}
cout << str << endl;
return 0;
}
```
输出结果为:
```
HELLO, WORLD!
```