C语言tolower函数用于把大写字母转换为小写字母。
在本文中,我们先来介绍tolower函数的使用方法,然后编写一个自定义的_tolower函数,实现与tolower函数相同的功能。
1、包含头文件
#include <ctype.h>
2、函数声明
int tolower(int c);
3、功能说明
把大写字母转换为小写字母,如果参数c不是大写字母就不转换,您可能会问:tolower函数的参数和返回值是整数,不是字符,在C语言中,字符就是整数,请补充学习一下基础知识。
参数c为待转换的字符。
返回值为转换后的结果。
4、示例
#include <stdio.h>
int main()
{
printf("tolower('-')=%cn",tolower('-'));
printf("tolower('0')=%cn",tolower('0'));
printf("tolower('a')=%cn",tolower('a'));
printf("tolower('A')=%cn",tolower('A'));
}
运行效果

5、自定义的tolower函数的实现方法
在以下示例中,把自定义的tolower函数命名为_tolower。
程序的逻辑是:判断参数c是否为大写字母,如果是则加上32(小写字母和大写字母的ASCII码值相差32),如果不是直接返回原字符。
#include <stdio.h>
// 自定义的tolower函数。
int _tolower(int c)
{
if (c>='A' && c<='Z') return c+32;
else return c;
}
int main()
{
printf("_tolower('-')=%cn",_tolower('-'));
printf("_tolower('0')=%cn",_tolower('0'));
printf("_tolower('a')=%cn",_tolower('a'));
printf("_tolower('A')=%cn",_tolower('A'));
}
运行效果

到此这篇关于介绍C语言中tolower函数的实例的文章就介绍到这了,更多相关C语言 tolower函数内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!
最后
以上就是花痴音响最近收集整理的关于介绍C语言中tolower函数的实例的全部内容,更多相关介绍C语言中tolower函数内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复