C语言tolower函数介绍、示例和实现

C语言tolower函数用于把大写字母转换为小写字母。

在本文中,我们先来介绍tolower函数的使用方法,然后编写一个自定义的_tolower函数,实现与tolower函数相同的功能。

1、包含头文件

#include <ctype.h>

2、函数声明

int tolower(int c);

3、功能说明

把大写字母转换为小写字母,如果参数c不是大写字母就不转换,您可能会问:tolower函数的参数和返回值是整数,不是字符,在C语言中,字符就是整数,请补充学习一下基础知识。

参数c为待转换的字符。

返回值为转换后的结果。

4、示例

/*
 * 程序名: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'));
}

运行效果
《C语言tolower函数介绍、示例和实现》

5、自定义的tolower函数的实现方法

在以下示例中,把自定义的tolower函数命名为_tolower。

程序的逻辑是:判断参数c是否为大写字母,如果是则加上32(小写字母和大写字母的ASCII码值相差32),如果不是直接返回原字符。

/*
 * 程序名: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语言tolower函数介绍、示例和实现》

6、版权声明

C语言技术网原创文章,转载请说明文章的来源、作者和原文的链接。

来源:C语言技术网(www.freecplus.net

作者:码农有道

如果这篇文章对您有帮助,请点赞支持,或在您的博客中转发此文,让更多的人可以看到它,谢谢!!!

    原文作者:C语言实战
    原文地址: https://blog.51cto.com/14793471/2490981
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞