把字符串中的大写字母改成小写字母,其他字符不变。 输入格式: 输入一个长度不超过20的包含大小写母和数字的字符串。 输出格式: 输出大写字母改成小写字母后的字符串,c语言
时间: 2024-02-03 12:14:25 浏览: 157
以下是一个将字符串中大写字母改为小写字母的C语言程序:
```c
#include <stdio.h>
int main()
{
char str[20];
int i = 0;
printf("请输入字符串:");
scanf("%s", str);
while (str[i] != '\0')
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
str[i] = str[i] + ('a' - 'A');
}
i++;
}
printf("转换后的字符串是:%s\n", str);
return 0;
}
```
该程序先读入一个字符串,然后遍历字符串中的每个字符,如果该字符是大写字母,则将其转换成小写字母。最后输出转换后的字符串。
相关问题
C语言把一个字符串中的大写字母改成小写字母,其他字符保持不变
在C语言中,可以使用标准库函数`tolower()`和`toupper()`来转换字母的大小写,但是这两个函数只能处理单个字符。如果你想将整个字符串中的大写字母转化为小写,而其他字符不变,你需要遍历字符串并逐个应用这个转换。下面是一个简单的示例,使用循环和`tolower()`函数:
```c
#include <ctype.h> // 包含转义函数头文件
#include <stdio.h>
void convert_to_lowercase(char *str) {
for (int i = 0; str[i] != '\0'; i++) { // 遍历直到遇到字符串结束符'\0'
if (isupper(str[i])) { // 检查当前字符是否为大写
str[i] = tolower(str[i]); // 转换为小写
}
}
}
int main() {
char input[] = "Hello World! This is a Test.";
convert_to_lowercase(input);
printf("转换后的字符串: %s\n", input);
return 0;
}
```
当你运行上面的程序,输入的字符串"Hello World! This is a Test."将会变为"hello world! this is a test."。
用c语言 字符串字母大小写转换 题目描述 编写程序,将字符串中小写字母字符转换为大写。 输入格式: 在一行中输入一个字符串(不超过 80 个字符)。 输出格式: 将字符串中小写字母字符转换为大写后输出。 输入样例: strings 输出样例: STRINGS
下面是使用C语言实现字符串字母大小写转换的代码:
```c
#include <stdio.h>
int main()
{
char str[80];
int i;
printf("请输入一个字符串:");
gets(str);
for(i = 0; str[i] != '\0'; i++)
{
if(str[i] >= 'a' && str[i] <= 'z')
{
str[i] -= 32; // 转换为大写字母
}
}
printf("转换后的字符串为:%s", str);
return 0;
}
```
思路:
1. 定义一个字符数组 str 来存储输入的字符串;
2. 使用 gets 函数从标准输入中读取字符串,存储到 str 中;
3. 遍历字符串 str,如果是小写字母,则将其转换为大写字母(小写字母的 ASCII 码值比大写字母的 ASCII 码值大 32);
4. 输出转换后的字符串。
注意:
1. gets 函数不安全,建议使用 fgets 函数代替;
2. 字符串的最后一个字符是 '\0',表示字符串的结束。遍历字符串时应该判断是否等于 '\0';
3. 如果字符串中包含非字母字符,可以不做处理,直接输出即可。
阅读全文
相关推荐














