ctype.h库函数
头文件ctype.h声明了一组用于分类和转换单个字符的函数。所有的函数都接收一个int型的参数,并返回一个int——返回的int可能代表一个字符,也可能代表的是bool值(0为假,非0为真)。
你可能会有疑问,既然是字符操作,接受的参数为什么不用char,而用int? Good question,答案我也不确定……O_O 好吧,我们继续……
因为这些“函数”太过常用,且调用频繁,所以为了提高效率,这些“函数”是由一些宏实现的,通过查看源文件可以很容易发现这一点。还有一点需要了解的是,如果修改了区域设置(默认为“C”区域设置),这些函数的行为可能会略有不同,不过不用担心,现在只需要知道有这么一个概念,后面介绍区域设置(locale.h)会再详细讨论。它们可以分为两组。一组用来判断字符是否属于某个分类,包括:
| 名称 | 签名 | 描述 |
|---|---|---|
| isalnum | int isalnum ( int c ); | 数字或字母 |
| isalpha | int isalpha ( int c ); | 字母,或者那些实现定义的字符集中iscntrl,isdigit,ispunct,isspace判定都不为真的字符。在“C”区域设置里,isalpha只是isupper,islower返回真的字符 |
| iscntrl | int iscntrl ( int c ); | 控制字符,与isprint相反,即不可打印字符 |
| isdigit | int isdigit ( int c ); | 十进制数字字符 |
| isgraph | int isgraph ( int c ); | 除空格(' ')以外的其他可打印(isprint为true)字符 |
| islower | int islower ( int c ); | 小写字母 |
| isprint | int isprint ( int c ); | 包括空格(' ')在内的打印字符 |
| ispunct | int ispunct ( int c ); | 除空格(' ')和isalnum判定为真的字符以外的所有打印字符 |
| isspace | int isspace ( int c ); | 空白字符 |
| isupper | int isupper ( int c ); | 大写字母 |
| isxdigit | int isxdigit ( int c ); | 16进制数字字符 |
另外一组用来转换大小写,包括:
| 名称 | 签名 | 描述 |
|---|---|---|
| toupper | int toupper ( int c ); | 转换c为大写 |
| tolower | int tolower ( int c ); | 转换c为小写 |
下面拿isalpha,toupper/tolower做示范,看如何使用这些函数,输出结果就不贴出来了,分不清大小写的童鞋自觉面壁>_<
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#include <stdlib.h> #include <stdio.h> #include <ctype.h> int main ( int argc, char *argv[] ) { int c = 'a'; int uc; if( isalpha( c ) ) { printf( "'%c' is an alphabet\n" ,c ); if( islower( c )) { uc = toupper( c ); printf( "uppercase: %c\n", uc ); } else { uc = tolower( c ); printf( "lowercase: %c\n", uc ); } } else { printf( "'%c' is not an alphabet\n" ,c ); } return EXIT_SUCCESS; } |
最后附一张默认的“C”区域设置下ASCII码对应的判定结果,可以帮助你对这些函数的判定结果有个直观的印象,enjoy it !
| ASCII | 字符 | iscntrl | isspace | isupper | islower | isalpha | isdigit | isxdigit | isalnum | ispunct | isgraph | isprint |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0x00 .. 0x08 | NUL, (其他控制字符) | x | ||||||||||
| 0x09 .. 0x0D | (空白字符代码: '\t','\f','\v','\n','\r') | x | x | |||||||||
| 0x0E .. 0x1F | (控制字符) | x | ||||||||||
| 0x20 | 空格 (' ') | x | x | |||||||||
| 0x21 .. 0x2F | !"#$%&'()*+,-./ | x | x | x | ||||||||
| 0x30 .. 0x39 | 01234567890 | x | x | x | x | x | ||||||
| 0x3a .. 0x40 | :;<=>?@ | x | x | x | ||||||||
| 0x41 .. 0x46 | ABCDEF | x | x | x | x | x | x | |||||
| 0x47 .. 0x5A | GHIJKLMNOPQRSTUVWXYZ | x | x | x | x | x | ||||||
| 0x5B .. 0x60 | [\]^_` | x | x | x | ||||||||
| 0x61 .. 0x66 | abcdef | x | x | x | x | x | x | |||||
| 0x67 .. 0x7A | ghijklmnopqrstuvwxyz | x | x | x | x | x | ||||||
| 0x7B .. 0x7E | {|}~ | x | x | x | ||||||||
| 0x7F | (DEL) | x |
首发自:http://yaohuiji.com/
欢迎转载和探讨学习,但转载时必须保留本文的署名Jack Yao及链接,未经许可请勿商业使用。
ctype.h库函数的更多相关文章
- ctype.h库函数----字符操作函数
在c++中使用时: #include <cctype> 字符判断函数 1.isalnum函数--判断是否是英文字母或数字字符,如果是,则返回非0值,如果不是,则返回0. 函数参数 :可以 ...
- C库函数手册(ctype.h)
ctype.h函数说明:int isalpha(int ch) 若ch是字母('A'-'Z','a'-'z')返回非0值,否则返回0 int isdigit(int ch) 若ch是数字('0'- ...
- c语言字符类别测试库函数#include<ctype.h>
字符类测试<ctype.h> 头文件<ctype.h>中说明了一些用于测试字符的函数.每个函数的变量均为int类型,变量的值必须是EOF或可用unsigned char类型表示 ...
- <ctype.h> C语言标准库
ctype.h是C标准函数库中的头文件,定义了一批C语言字符分类函数(C character classification functions),用于測试字符是否属于特定的字符类别.如字母字符.控制字 ...
- ctype.h 第2章
ctype.h ctype.h是c标准函数库中的头文件 定义了一批c语言字符分类函数 (c character classification functions) 用于测试字符是否属于特定的字 ...
- C标准头文件<ctype.h>
主要包括了一些字符识别和转换函数 字符判断 isalnum() //函数原型 #include<ctype.h> int isalum(int c); 功能:如果输入的字符是字母(alph ...
- C 标准库系列之ctype.h
ctype.h 主要提供了一些函数用以测试字符或字符处理的功能函数:包括字符判断检测.字符转换: 目前ASCII字符可分为以下一些类型,如:大写.小写.字母.数字.十六进制.空白字符.可打印字符.控制 ...
- c 头文件<ctype.h>(二)
测试<ctype.h>函数 #include <stdio.h> #include <ctype.h> int main(){ ; ; i < ; ++i){ ...
- c 头文件<ctype.h>(一)
头文件<ctype.h>中声明了一些测试字符的函数. 每个函数的参数均为int类型,参数的值必须是EOF或可用unsigned char类型表示的字符,函数返回值为int类型. 如果参数c ...
随机推荐
- C# 给自己的代码 添加上 自己的版权信息
如何将自己的代码自动添加版权信息 现在大多数公司都规定程序员在程序文件的头部加上版权信息,这样每个人写的文件都可以区分开来,如果某个文件出现问题就可以快速的找到文件的创建人,用最短的时间来解决问题,常 ...
- sublime快捷键收藏
快速查找(ctrl + P)输入@+函数名可以快速找到函数.输入#+文本可以快速进行文件内文本匹配.3. 多行游标功能(ctrl + D,非常实用)如何将文件中的某个单词更改为另一个?方法一:利用查找 ...
- Bootstrap 模态对话框只加载一次 remote 数据的解决办法
原文: https://my.oschina.net/qczhang/blog/190215?p=1
- Planner – 项目管理软件
http://www.appinn.com/planner/ Planner 是一款开源.易用.跨平台的项目管理软件.@appinn 二猪用了 OpenProject 几年,现在已经受够了它的各种 ...
- C# 伪造 referer 提交数据
private string SendRequest(string account, string cardNumber, string cardPass) { string targetUrl = ...
- shell字符串
字符串是shell编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号.单双引号的区别跟PHP类似. 单引号 str='this ...
- Linux系统编程(23)——信号的阻塞
实际执行信号的处理动作称为信号递达(Delivery),信号从产生到递达之间的状态,称为信号未决(Pending).进程可以选择阻塞(Block)某个信号.被阻塞的信号产生时将保持在未决状态,直到进程 ...
- EasyUI的下拉选择框控件方法被屏蔽处理方式
1.html标签如下 <div id="selectMap" style="top: 1px;left: 80px;position: absolute;" ...
- UESTC_Sea Base Exploration CDOJ 409
When the scientists explore the sea base, they use a kind of auto mobile robot, which has the missio ...
- Implement Stack using Queues 解答
Question Implement the following operations of a stack using queues. push(x) -- Push element x onto ...