c语言将字符串中的大写字母转换为对应的小写字母
时间: 2023-12-12 14:06:09 浏览: 58
可以使用C语言中的标准库函数`tolower()`将字符串中的大写字母转换为小写字母。下面是一个示例代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "HELLO WORLD";
int i;
for(i = 0; str[i]; i++){
str[i] = tolower(str[i]);
}
printf("转换后的字符串为: %s", str);
return 0;
}
```
输出结果为:
```
转换后的字符串为: hello world
```
在上面的代码中,我们使用了`tolower()`函数将字符串中的大写字母转换为小写字母。在循环中,我们遍历字符串中的每个字符,并调用`tolower()`函数将其转换为小写字母。最后打印出转换后的字符串。
相关问题
将字符串中的大写字母转换为对应的小写字母c语言
可以使用C语言中的标准库函数tolower()将字符串中的大写字母转换为对应的小写字母。tolower()函数需要包含头文件<ctype.h>。
下面是一个示例代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello, World!";
int i = 0;
while(str[i]) {
str[i] = tolower(str[i]);
i++;
}
printf("%s", str);
return 0;
}
```
输出结果为:
```
hello, world!
```
C语言将字符串中的大写字母转换成小写字母
在C语言中,可以使用库函数toupper()将字符转换为大写字母,也可以使用库函数tolower()将字符转换为小写字母。如果需要将字符串中的所有大写字母转换为小写字母,可以使用循环遍历字符串中的每一个字符,然后使用tolower()函数将大写字母转换成小写字母。以下是一个示例代码:
```c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char str[100];
int i;
printf("Enter a string: ");
gets(str);
for (i = 0; str[i] != '\0'; i++) {
if (isupper(str[i])) {
str[i] = tolower(str[i]);
}
}
printf("String in lowercase: %s", str);
return 0;
}
```
在上面的代码中,我们使用了gets()函数来获取用户输入的字符串,并且使用了isupper()函数来检查每一个字符是否为大写字母。如果该字符是大写字母,我们就使用tolower()函数将其转换为小写字母。最后,我们使用printf()函数输出转换后的字符串。