编写C语言程序:将字符串str中的小写字母全部转换成大写字母,大写字母全部转换成小写字母,其他字符不变
时间: 2024-05-12 18:18:37 浏览: 18
很高兴为您解答这个问题,请看下面的C语言代码:
```c
#include <stdio.h>
#include <ctype.h>
void convert(char *str) {
while(*str != '\0') {
if (islower(*str)) {
*str = toupper(*str);
} else if (isupper(*str)) {
*str = tolower(*str);
}
str++;
}
}
int main() {
char str[100];
printf("请输入字符串: ");
fgets(str, 100, stdin);
convert(str);
printf("转换后的字符串: %s", str);
return 0;
}
```
这个程序中,我们使用了ctype库中的islower、toupper、isupper和tolower函数。islower和isupper函数用于判断字符是否为小写字母和大写字母,而toupper和tolower函数用于将字符转换为大写字母和小写字母。
相关问题
C语言编写程序,将字符串中小写字母字符转换为大写。
以下是一个简单的C程序,可以将字符串中的小写字母全部转换为大写字母:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[1000];
printf("请输入字符串:");
fgets(str, sizeof(str), stdin); // 从标准输入读取字符串
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (islower(str[i])) { // 判断是否为小写字母
str[i] = toupper(str[i]); // 将小写字母转换为大写字母
}
}
printf("转换后的字符串为:%s", str);
return 0;
}
```
这个程序首先从标准输入读取一个字符串,然后遍历字符串的每一个字符,如果发现是小写字母,就使用 `toupper()` 函数将它转换为大写字母。最后输出转换后的字符串。
用c语言编写程序,将字符串中小写字母字符转换为大写。 输入样例 strings 输出样例 STRINGS
好的,以下是用 C 语言编写的程序,可以实现将字符串中的小写字母字符转换为大写:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int len, i;
printf("请输入字符串:");
scanf("%s", str);
len = strlen(str);
for (i = 0; i < len; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 'a' + 'A';
}
}
printf("转换后的字符串为:%s\n", str);
return 0;
}
```
这段程序可以读入一个字符串,然后遍历字符串中的每个字符,如果字符是小写字母,就将其转换为大写字母。最后输出转换后的字符串。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)