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 ...
随机推荐
- PHP怎么实现网站中,同一个用户不能同时在线?
先上图,看个大概: 一般的原则就是,后一个用户登录时会把前一个用户踢下线. 在用户首次登录时,我们会把用户的sessionid保存到数据库,这个是用户的唯一标识.方便后边操作. 用户只有在登录时才会和 ...
- php常用的header头
<?php /** * php常用的header头设置... */ header('HTTP/1.1 200 OK'); // ok 正常访问 header('HTTP/1.1 404 Not ...
- POJ Find The Multiple 1426 (搜索)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22576 Accepted: 929 ...
- LINQ to SQL和Entity Framework对比与关联 (转载)
LINQ to SQL和Entity Framework对比与关联 LINQ to SQL和Entity Framework都是一种包含LINQ功能的对象关系映射技术.他们之间的本质区别在 ...
- UESTC_邱老师玩游戏 2015 UESTC Training for Dynamic Programming<Problem G>
G - 邱老师玩游戏 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- Binary Tree Paths 解答
Question Given a binary tree, return all root-to-leaf paths. For example, given the following binary ...
- Isomorphic Strings 解答
Question Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- ZOJ2913Bus Pass(BFS+set)
Bus Pass Time Limit: 5 Seconds Memory Limit: 32768 KB You travel a lot by bus and the costs of ...
- devStack
1,devstack shell 脚本开源官网 http://devstack.org/ 脚本功能快速搭建 OpenStack 的运行和开发环境 [Note tips by Ruiy devstack ...
- 一张图讲解为什么需要自己搭建自己的git服务以及搭建的途径
图片信息量有点大.不废话上图 图中的一些链接: gitlab官方安装文档 https://github.com/gitlabhq/gitlabhq/blob/master/doc/install/in ...