用C语言将一个字符串中的大写字母转为小写字母并将小写字母转为大写
时间: 2024-03-08 19:46:10 浏览: 24
您好!感谢提问。
下面是一个将字符串中的大写字母转为小写字母,小写字母转为大写字母的示例代码:
```c
#include <stdio.h>
#include <ctype.h>
void switch_case(char *str) {
while (*str) {
if (isupper(*str)) {
*str = tolower(*str);
} else if (islower(*str)) {
*str = toupper(*str);
}
str++;
}
}
int main() {
char str[] = "HeLLo WoRLd!";
printf("Before: %s\n", str);
switch_case(str);
printf("After: %s\n", str);
return 0;
}
```
这个程序中使用了 `isupper` 和 `islower` 函数来判断字符大小写。如果是大写字母,就使用 `tolower` 函数将其转为小写字母;如果是小写字母,就使用 `toupper` 函数将其转为大写字母。
希望能够帮到您!
相关问题
用C语言将字符串中大写字母转为小写字母,小写字母转为大写字母的代码
以下是将字符串中大写字母转为小写字母,小写字母转为大写字母的 C 代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i;
printf("请输入一个字符串:");
fgets(str, 100, stdin);
for (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;
}
```
这段代码使用了 ctype.h 头文件中的 isupper()、tolower()、islower() 和 toupper() 函数,分别用于判断字符是否为大写字母、将字符转换为小写字母、判断字符是否为小写字母和将字符转换为大写字母。
用c语言将一个字符串中的大写字母转换成小写字母输出
可以使用C语言中的标准库函数`tolower()`来实现将大写字母转换成小写字母的操作。
下面是一个示例代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello, WORLD!";
int i;
printf("Original string: %s\n", str);
for(i = 0; str[i]; i++) {
str[i] = tolower(str[i]);
}
printf("Lowercase string: %s\n", str);
return 0;
}
```
输出结果:
```
Original string: Hello, WORLD!
Lowercase string: hello, world!
```
在上面的代码中,我们首先定义一个字符串`str`,然后使用`tolower()`函数将其中的大写字母转换成小写字母,并将结果存储回原字符串中,最后输出转换后的字符串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.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)