实现字符串中的大写字母转换为小写字母,小写字母转换为大写字母,其他字符保持不变的C语言完整代码
时间: 2024-05-01 18:22:39 浏览: 14
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i;
printf("Enter a string: ");
scanf("%[^\n]", str);
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("Converted string: %s", str);
return 0;
}
```
注:这里使用了 C 标准库中的 `isupper()` 函数和 `tolower()` 函数,以及 `islower()` 函数和 `toupper()` 函数,用于判断字符是否为大写字母或小写字母,并进行大小写转换。
相关问题
c语言,将字符串中的小写字母转换为对应的大写字母 其他字符不变
可以使用C语言中的字符数组和循环结构来实现将字符串中的小写字母转换为对应的大写字母,其他字符不变。可以使用标准库函数toupper()将小写字母转换为大写字母。
下面是一个示例代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i = 0;
printf("请输入一个字符串:");
scanf("%s", str);
while (str[i]) {
if (islower(str[i])) { // 判断是否为小写字母
str[i] = toupper(str[i]); // 将小写字母转换为大写字母
}
i++;
}
printf("转换后的字符串为:%s\n", str);
return 0;
}
```
运行示例:
```
请输入一个字符串:hello world
转换后的字符串为:HELLO WORLD
```
c语言实现编程实现将输入的字符串中的大写字母转换为小写字母,小写字母转换为大写字母
可以使用C语言中的字符处理函数`ctype.h`库中的`toupper`和`tolower`函数来实现大写字母转小写字母,小写字母转大写字母的操作。具体实现代码如下:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
printf("请输入一个字符串:");
gets(str); // 输入字符串
int i = 0;
while (str[i]) { // 遍历字符串中的每一个字符
if (isupper(str[i])) // 如果是大写字母
str[i] = tolower(str[i]); // 转换为小写字母
else if (islower(str[i])) // 如果是小写字母
str[i] = toupper(str[i]); // 转换为大写字母
i++; // 移动到下一个字符
}
printf("转换后的字符串为:%s\n", str);
return 0;
}
```
其中,`isupper`函数用于判断字符是否为大写字母,返回值为非零值表示是大写字母,否则为小写字母;`tolower`函数用于将大写字母转换为小写字母;`islower`函数用于判断字符是否为小写字母,返回值为非零值表示是小写字母,否则为大写字母;`toupper`函数用于将小写字母转换为大写字母。