c加加字符串字母大小写转换
时间: 2024-11-20 18:26:45 浏览: 49
C++语言中,你可以使用标准库提供的`std::transform`函数结合`std::tolower`和`std::toupper`来实现字符串字母的大小写转换。这两个函数分别用于将字符转换为小写和大写。
下面是一个简单的例子:
```cpp
#include <algorithm> // 包含transform函数
#include <cctype> // 包含tolower和toupper函数
#include <string>
// 将字符串中的所有字符转为小写
std::string toLower(const std::string& str) {
std::string result = str;
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
return result;
}
// 将字符串中的所有字符转为大写
std::string toUpper(const std::string& str) {
std::string result = str;
std::transform(result.begin(), result.end(), result.begin(), ::toupper);
return result;
}
```
如果你想在原地修改字符串,可以传递`result`的引用而不是复制一份:
```cpp
void transformToLower(std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
}
void transformToUpper(std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
}
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)