服务器之家

服务器之家 > 正文

介绍C语言中tolower函数的实例

时间:2021-11-21 16:18     来源/作者:C语言技术网-码农有道

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

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

1、包含头文件

?
1
#include <ctype.h>

2、函数声明

?
1
int tolower(int c);

3、功能说明

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

参数c为待转换的字符。

返回值为转换后的结果。

4、示例

?
1
2
3
4
5
6
7
8
9
#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),如果不是直接返回原字符。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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函数的实例

到此这篇关于介绍C语言中tolower函数的实例的文章就介绍到这了,更多相关C语言 tolower函数内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/wucz122140729/article/details/105710187

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部