3.输入一行字符串,将该字符串中所有的大写字母改为小写字母后输出。用C语言
时间: 2024-03-12 09:45:53 浏览: 16
可以使用C语言中的字符处理函数`tolower()`将大写字母转换为小写字母,具体实现如下:
```c
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
int i;
printf("请输入字符串:");
gets(str);
for (i = 0; str[i] != '\0'; i++)
{
if (isupper(str[i])) // 判断是否为大写字母
str[i] = tolower(str[i]); // 转换为小写字母
}
printf("转换后的字符串为:%s", str);
return 0;
}
```
输入一个字符串后,该程序会将其中所有的大写字母转换为小写字母并输出。
相关问题
利用c语言输入一行字符串,将该字符串中所有的大写字母改为小写字母后输出。
好的,可以使用C语言的字符串处理库函数 `ctype.h` 中的 `tolower()` 函数将大写字母转换成小写字母,具体代码如下:
```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]); // 转换为小写字母
}
}
printf("转换后的字符串为:%s", str);
return 0;
}
```
这个程序中,我们使用了 `fgets()` 函数从标准输入中读入一行字符串,然后遍历字符串中的每个字符,如果是大写字母就使用 `tolower()` 函数将其转换为小写字母,最后输出转换后的字符串。
c语言实现输入一行字符串,将该字符串中所有的大写字母改为小写字母后输出
可以使用C语言中的字符串处理函数`tolower()`将大写字母转换为小写字母。具体实现如下:
```c
#include <stdio.h>
#include <ctype.h> // 包含tolower()函数
int main()
{
char str[100];
int i = 0;
printf("请输入字符串:");
fgets(str, 100, stdin); // 使用fgets()函数读取一行字符串
while (str[i] != '\0') // 遍历字符串
{
if (str[i] >= 'A' && str[i] <= 'Z') // 判断是否为大写字母
{
str[i] = tolower(str[i]); // 转换为小写字母
}
i++;
}
printf("转换后的字符串为:%s", str);
return 0;
}
```
其中,`fgets()`函数用于读取一行字符串,`tolower()`函数用于将大写字母转换为小写字母。