温馨提示×

c语言大写字母怎么转小写字母

小亿
106
2023-11-21 21:15:14
栏目: 编程语言

在C语言中,可以使用库函数toupper()和tolower()来将大写字母转换为小写字母。

toupper()函数接受一个大写字母作为参数,返回对应的小写字母,如果参数不是大写字母,则原样返回。

tolower()函数接受一个小写字母作为参数,返回对应的大写字母,如果参数不是小写字母,则原样返回。

以下是使用toupper()和tolower()函数进行大小写转换的示例代码:

#include <stdio.h>
#include <ctype.h>

int main() {
    char ch = 'A';  // 大写字母
    char lowerCh = tolower(ch);  // 转换为小写字母
    printf("转换前的字符: %c\n", ch);
    printf("转换后的字符: %c\n", lowerCh);
    
    ch = 'a';  // 小写字母
    char upperCh = toupper(ch);  // 转换为大写字母
    printf("转换前的字符: %c\n", ch);
    printf("转换后的字符: %c\n", upperCh);
    
    return 0;
}

输出结果为:

转换前的字符: A
转换后的字符: a
转换前的字符: a
转换后的字符: A

这样就可以通过toupper()和tolower()函数来实现大写字母转小写字母和小写字母转大写字母的转换。

0