C语言tolower函数用于把大写字母转换为小写字母。ide
在本文中,咱们先来介绍tolower函数的使用方法,而后编写一个自定义的_tolower函数,实现与tolower函数相同的功能。函数
#include <ctype.h>
int tolower(int c);
把大写字母转换为小写字母,若是参数c不是大写字母就不转换,您可能会问:tolower函数的参数和返回值是整数,不是字符,在C语言中,字符就是整数,请补充学习一下基础知识。学习
参数c为待转换的字符。.net
返回值为转换后的结果。code
/* * 程序名:book.c,此程序演示C语言的tolower函数。 * 做者:C语言技术网(www.freecplus.net) 日期:20190525 */ #include <stdio.h> int main() { printf("tolower('-')=%c\n",tolower('-')); printf("tolower('0')=%c\n",tolower('0')); printf("tolower('a')=%c\n",tolower('a')); printf("tolower('A')=%c\n",tolower('A')); }
运行效果blog
在如下示例中,把自定义的tolower函数命名为_tolower。图片
程序的逻辑是:判断参数c是否为大写字母,若是是则加上32(小写字母和大写字母的ASCII码值相差32),若是不是直接返回原字符。get
/* * 程序名:book.c,此程序演示C语言自定义的tolower函数。 * 做者:C语言技术网(www.freecplus.net) 日期:20190525 */ #include <stdio.h> // 自定义的tolower函数。 int _tolower(int c) { if (c>='A' && c<='Z') return c+32; else return c; } int main() { printf("_tolower('-')=%c\n",_tolower('-')); printf("_tolower('0')=%c\n",_tolower('0')); printf("_tolower('a')=%c\n",_tolower('a')); printf("_tolower('A')=%c\n",_tolower('A')); }
运行效果博客
C语言技术网原创文章,转载请说明文章的来源、做者和原文的连接。it
来源:C语言技术网(www.freecplus.net)
做者:码农有道
若是这篇文章对您有帮助,请点赞支持,或在您的博客中转发此文,让更多的人能够看到它,谢谢!!!