c++ 把字母大小写切换
时间: 2023-08-03 14:59:56 浏览: 40
你可以使用 `toupper()` 和 `tolower()` 函数来切换 C++ 字符串中的字母大小写。以下是一个示例代码:
```cpp
#include <iostream>
#include <cctype>
using namespace std;
int main() {
string str = "Hello World!";
for (int i = 0; i < str.length(); i++) {
if (islower(str[i])) {
str[i] = toupper(str[i]);
} else if (isupper(str[i])) {
str[i] = tolower(str[i]);
}
}
cout << str << endl;
return 0;
}
```
上述代码会将字符串 `str` 中的大写字母转换为小写字母,将小写字母转换为大写字母,并输出结果。在这个例子中,输出将会是 "hELLO wORLD!"。你可以根据需要修改代码来适应你的实际需求。
相关问题
c++判断字母大小写
在C++中,可以使用标准库函数`isupper()`和`islower()`来判断一个字符是否为大写或小写字母。这两个函数都需要传入一个字符参数,返回值为布尔类型,表示是否为大写或小写字母。示例代码如下:
```c++
#include <iostream>
#include <cctype>
int main() {
char ch = 'A';
if (isupper(ch)) {
std::cout << ch << " is an uppercase letter." << std::endl;
} else {
std::cout << ch << " is not an uppercase letter." << std::endl;
}
ch = 'b';
if (islower(ch)) {
std::cout << ch << " is a lowercase letter." << std::endl;
} else {
std::cout << ch << " is not a lowercase letter." << std::endl;
}
return 0;
}
```
输出结果为:
```
A is an uppercase letter.
b is a lowercase letter.
```
c++字母大小写转换
在C++中,可以使用toupper()和tolower()函数来进行字母大小写转换。这两个函数都需要一个字符作为参数,并返回转换后的字符。toupper()函数将小写字母转换为大写字母,而tolower()函数则将大写字母转换为小写字母。需要注意的是,这些函数只会对字母字符进行大小写转换,对于非字母字符,它们不会产生任何影响。
下面是一个简单的示例代码,演示了如何使用toupper()和tolower()函数进行字母大小写转换:
```
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch = 'a';
cout << "Original character: " << ch << endl;
// Convert to uppercase
ch = toupper(ch);
cout << "Uppercase character: " << ch << endl;
// Convert to lowercase
ch = tolower(ch);
cout << "Lowercase character: " << ch << endl;
return 0;
}
```