头文件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库函数的更多相关文章

  1. ctype.h库函数----字符操作函数

    在c++中使用时:  #include <cctype> 字符判断函数 1.isalnum函数--判断是否是英文字母或数字字符,如果是,则返回非0值,如果不是,则返回0. 函数参数 :可以 ...

  2. C库函数手册(ctype.h)

    ctype.h函数说明:int isalpha(int ch)  若ch是字母('A'-'Z','a'-'z')返回非0值,否则返回0 int isdigit(int ch)  若ch是数字('0'- ...

  3. c语言字符类别测试库函数#include<ctype.h>

    字符类测试<ctype.h> 头文件<ctype.h>中说明了一些用于测试字符的函数.每个函数的变量均为int类型,变量的值必须是EOF或可用unsigned char类型表示 ...

  4. &lt;ctype.h&gt; C语言标准库

    ctype.h是C标准函数库中的头文件,定义了一批C语言字符分类函数(C character classification functions),用于測试字符是否属于特定的字符类别.如字母字符.控制字 ...

  5. ctype.h 第2章

    ctype.h ctype.h是c标准函数库中的头文件   定义了一批c语言字符分类函数   (c character classification functions) 用于测试字符是否属于特定的字 ...

  6. C标准头文件<ctype.h>

    主要包括了一些字符识别和转换函数 字符判断 isalnum() //函数原型 #include<ctype.h> int isalum(int c); 功能:如果输入的字符是字母(alph ...

  7. C 标准库系列之ctype.h

    ctype.h 主要提供了一些函数用以测试字符或字符处理的功能函数:包括字符判断检测.字符转换: 目前ASCII字符可分为以下一些类型,如:大写.小写.字母.数字.十六进制.空白字符.可打印字符.控制 ...

  8. c 头文件<ctype.h>(二)

    测试<ctype.h>函数 #include <stdio.h> #include <ctype.h> int main(){ ; ; i < ; ++i){ ...

  9. c 头文件<ctype.h>(一)

    头文件<ctype.h>中声明了一些测试字符的函数. 每个函数的参数均为int类型,参数的值必须是EOF或可用unsigned char类型表示的字符,函数返回值为int类型. 如果参数c ...

随机推荐

  1. jquery find()和filter()的区别

    在写js查找当前标签下某一子元素的子元素集合时,用到了此方法 $(".flyout-trigger").mouseover(function(){ var maxnum=$(thi ...

  2. php5.5新特性之yield理解

    今天,在阅读别人代码时,其中出现了一个陌生的关键字yield,想一探究竟,于是找到:http://php.net/manual/zh/language.generators.overview.php ...

  3. MVC4商城项目三:分部视图在导航条上的应用

    写了几天发觉大部分时间用在JS上了,本来想写个musicstore,却加了框架,然后又想用后台,然后又想用上bootstrapt,然后又想弄权限设计,然后又想………… 看来是想多了~ 好吧,最近把后台 ...

  4. [l转]VLM_on_Linux

    Instructions for running the Symbolics VLM virtual machine on Linux. VLM On Linux From LispMachinery ...

  5. socket基础示例(一)

    //Socket基本编程 //服务端: using System.Net; using System.Net.Sockets; using System.Text; using System.Thre ...

  6. javascript之DOM操作

    一.document.getElementById()    根据Id获取元素节点 <div id="div1"> <p id="p1"> ...

  7. C++ 11学习(1):lambda表达式

    转载请注明,来自:http://blog.csdn.net/skymanwu #include <iostream> #include <vector> #include &l ...

  8. Light OJ 1067 Combinations (乘法逆元)

    Description Given n different objects, you want to take k of them. How many ways to can do it? For e ...

  9. ubuntu centos debina

    文章目录CentOSDebianCentOS与Debian哪个好?Ubuntu买VPS时有CentOS.Debian.Ubuntu三种操作系统可以选择,Linux下哪种系统更好是很多新人都会遇到的问题 ...

  10. 利用FreeMarker静态化网页

    1.介绍-FreeMarker是什么 模板引擎:一种基于模板的.用来生成输出文本的通用工具 基于Java的开发包和类库 2.介绍-FreeMarker能做什么 MVC框架中的View层组件 Html页 ...