C语言实验——大小写转换 题目描述 把一个字符串里所有的大写字母换成小写字母,小写字母换成大写字母。其他字符保持不变。 输入 输入为一行字符串,其中不含空格。长度不超过80个字符。 输出 输出转换好的字符串 用C语言编写
时间: 2023-12-03 08:42:21 浏览: 118
以下是实现大小写转换的C语言代码:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[81];
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] - 32; //小写字母转换为大写字母
}
else if (str[i] >= 'A' && str[i] <= 'Z')
{
str[i] = str[i] + 32; //大写字母转换为小写字母
}
//其他字符不变
}
printf("转换后的字符串为:%s\n", str);
return 0;
}
```
这段代码中,我们先通过`scanf()`函数读入一行字符串,然后使用`strlen()`函数计算出字符串长度。接着使用`for`循环遍历字符串的每一个字符,判断它是否为大写字母或小写字母,然后进行相应的转换。最后输出转换后的字符串即可。
相关问题
c语言将字符串中的小写字母转换成大写字母,将原本的大写字母转换成小写字母
可以使用C语言中的toupper和tolower函数来实现字符串中大小写字母的转换。具体实现方法可以参考以下代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello, World!";
int i;
for (i = 0; str[i] != '\0'; i++) {
if (islower(str[i])) {
str[i] = toupper(str[i]);
} else if (isupper(str[i])) {
str[i] = tolower(str[i]);
}
}
printf("%s\n", str);
return 0;
}
```
这段代码可以将字符串中的小写字母转换成大写字母,将原本的大写字母转换成小写字母,并输出转换后的字符串。
用c语言将输入的字符串中所有大写字母换成小写
以下是将输入字符串中所有大写字母替换成小写的示例C代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i = 0;
printf("请输入字符串:");
fgets(str, sizeof(str), stdin);
while (str[i]) {
str[i] = tolower(str[i]);
i++;
}
printf("转换后的字符串为:%s", str);
return 0;
}
```
代码解释:
1. 首先,我们声明一个字符数组 `str` 用来存储输入的字符串,以及一个变量 `i` 用来遍历该字符串。
2. 然后,我们使用 `fgets` 函数从标准输入中读取字符串,并将其存储在 `str` 中。
3. 接着,我们使用一个循环遍历 `str` 中的每个字符,并使用 `tolower` 函数将其转换为小写字母。
4. 最后,我们输出转换后的字符串。
需要注意的是,我们使用了 `ctype.h` 头文件中的 `tolower` 函数来进行字符大小写转换。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)