C++大小写字母转换运算符
时间: 2024-04-30 10:14:42 浏览: 11
C++中的大小写字母转换运算符是指toupper和tolower函数。这两个函数可以用来将字符转换为大写或小写形式。
toupper函数接受一个字符作为参数,并返回将该字符转换为大写形式后的结果。例如,toupper('a')的结果是'A'。
tolower函数也接受一个字符作为参数,并返回将该字符转换为小写形式后的结果。例如,tolower('A')的结果是'a'。
这两个函数都需要包含头文件<cctype>才能使用。
相关问题
c++ 大小写字母转换
以下是C++中大小写字母转换的例子:
```c++
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string str;
cout << "请输入一个含大小写的字符串:";
cin >> str;
// 转小写
transform(str.begin(), str.end(), str.begin(), ::tolower);
cout << "转化为小写后为:" << str << endl;
// 转大写
transform(str.begin(), str.end(), str.begin(), ::toupper);
cout << "转化为大写后为:" << str << endl;
return 0;
}
```
c++大小写字母转换代码
以下是一个简单的C++大小写字母转换代码示例:
```c++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string str = "Hello World";
transform(str.begin(), str.end(), str.begin(), ::tolower); // 将字符串中的大写字母转换为小写字母
cout << str << endl; // 输出 hello world
transform(str.begin(), str.end(), str.begin(), ::toupper); // 将字符串中的小写字母转换为大写字母
cout << str << endl; // 输出 HELLO WORLD
return 0;
}
```
这个示例使用了 `transform` 函数,它可以将一个序列中的每个元素应用一个函数,并将结果存储到另一个序列中。在这个示例中,我们将 `str` 字符串中的每个字符应用 `::tolower` 或 `::toupper` 函数,从而实现大小写字母的转换。