c++把一个字符串里所有的大写字母换成小写字母,小写字母换成大写字母。其他字符保持不变。
时间: 2023-05-17 07:05:25 浏览: 70
可以使用以下代码实现:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
printf("请输入一个字符串:");
fgets(str, 100, stdin);
for (int i = 0; str[i] != '\0'; i++) {
if (isupper(str[i])) {
str[i] = tolower(str[i]);
} else if (islower(str[i])) {
str[i] = toupper(str[i]);
}
}
printf("转换后的字符串为:%s", str);
return 0;
}
```
这段代码使用了 `isupper` 和 `islower` 函数来判断字符是否为大写字母或小写字母,使用了 `tolower` 和 `toupper` 函数来进行大小写转换。
相关问题
将输入的字符串中所有大写字母换成小写用c++写
可以使用标准库函数`std::tolower`来实现将大写字母换成小写字母的操作。以下是一个示例代码:
```cpp
#include <iostream>
#include <string>
#include <cctype>
int main() {
std::string input;
std::cout << "请输入字符串:";
std::getline(std::cin, input);
for (char& c : input) {
c = std::tolower(c);
}
std::cout << "转换后的字符串为:" << input << std::endl;
return 0;
}
```
在上面的代码中,我们首先使用`std::getline`函数读取用户输入的字符串,然后使用`std::tolower`函数将字符串中所有的大写字母转换成小写字母。最后输出转换后的字符串。
给定一个字符串,将其中所有的小写字母转换成大写字母。C++
可以使用 C++ 中的 toupper 函数将小写字母转换成大写字母,具体实现如下:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char s[] = "Hello, World!";
int len = strlen(s);
for (int i = 0; i < len; i++) {
if (s[i] >= 'a' && s[i] <= 'z') { // 如果是小写字母
s[i] = toupper(s[i]); // 转换成大写字母
}
}
cout << s << endl; // 输出转换后的字符串
return 0;
}
```
输出结果为:
```
HELLO, WORLD!
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)