整理:<cctype>(<ctype.h>)


int isalpha(int ch)  若ch是字母('A'-'Z','a'-'z')返回非0值,否则返回0
int isalnum(int ch)  若ch是字母('A'-'Z','a'-'z')或数字('0'-'9')
                     返回非0值,否则返回0
int isascii(int ch)  若ch是字符(ASCII码中的0-127)返回非0值,否则返回0
int iscntrl(int ch)  若ch是作废字符(0x7F)或普通控制字符(0x00-0x1F)
                     返回非0值,否则返回0
int isdigit(int ch)  若ch是数字('0'-'9')返回非0值,否则返回0
int isgraph(int ch)  若ch是可打印字符(不含空格)(0x21-0x7E)返回非0值,否则返回0
int islower(int ch)  若ch是小写字母('a'-'z')返回非0值,否则返回0
int isprint(int ch)  若ch是可打印字符(含空格)(0x20-0x7E)返回非0值,否则返回0
int ispunct(int ch)  若ch是标点字符(0x00-0x1F)返回非0值,否则返回0
int isspace(int ch)  若ch是空格(' '),水平制表符(' '),回车符(' '),
                     走纸换行('f'),垂直制表符('v'),换行符(' ')
                     返回非0值,否则返回0
int isupper(int ch)  若ch是大写字母('A'-'Z')返回非0值,否则返回0
int isxdigit(int ch) 若ch是16进制数('0'-'9','A'-'F','a'-'f')返回非0值,
                     否则返回0
int tolower(int ch)  若ch是大写字母('A'-'Z')返回相应的小写字母('a'-'z')
int toupper(int ch)  若ch是小写字母('a'-'z')返回相应的大写字母('A'-'Z')



ctype.h的C标准库的头文件中提供的声明几个有用的函数测试和字符映射。

所有的功能都接受int作为参数,其值必须是EOF或为unsigned char表示。

所有函数返回的参数c非零(true),如果满足条件。否则返回0。

库函数

以下是在头文件ctype.h中定义的函数:

S.N. 函数及说明
1 int isalnum(int c)
该函数检查传递的字符是否是字母数字。
2 int isalpha(int c)
该函数是否传递的字符是字母。
3 int iscntrl(int c)
该函数是否传递的字符是控制字符。
4 int isdigit(int c)
该函数是否传递的字符是十进制数字。
5 int isgraph(int c)
该函数是否传递的字符的图形表示,使用的语言环境。
6 int islower(int c)
该函数检查传递的字符是否是小写字母。
7 int isprint(int c)
该函数检查传递的字符是否是可打印的。
8 int ispunct(int c)
该函数检查传递的字符是否是标点符号。
9 int isspace(int c)
该函数检查传递的字符是否是空白。
10 int isupper(int c)
该函数检查传递的字符是否是大写字母。
11 int isxdigit(int c)
该函数检查传递的字符是否是十六进制数字。

该库还包含两个转换函数,也接受并返回一个“整数”

S.N. 函数及说明
1 int tolower(int c)
这个函数转换大写字母为小写。
2 int toupper(int c)
这个函数小写字母转换为大写。

字符类

S.N. 字符类说明
1 Digits
A set of whole numbers { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
2 Hexadecimal digits
This is the set of { 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f }
3 Lowercase letters
This is a set of { a b c d e f g h i j k l m n o p q r s t u v w x y z }
4 Uppercase letters
A set of whole numbers {A B C D E F G H I J K L M N O P Q R S T U V W X Y Z }
5 Letters
This is a set of lowercase letters and uppercase letters
6 Alphanumeric characters
This is a set of Digits, Lowercase letters and Uppercase letters
7 Punctuation characters
This is a set of ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ ] ^ _ ` { | } ~
8 Graphical characters
This is a set of Alphanumeric characters and Punctuation characters.
9 Space characters
This is a set of tab, newline, vertical tab, form feed, carriage return, and space.
10 Printable characters
This is a set of Alphanumeric characters, Punctuation characters and Space characters.
11 Control characters
In ASCII, these characters have octal codes 000 through 037, and 177 (DEL).
12 Blank characters
These are space and tab.
13 Alphabetic characters
This is a set of Lowercase letters and Uppercase letters.

C++ 语言下
头文件:#include <cctype>
说明: 字符处理库
功能一:字符测试
1> 函数原型均为int isxxxx(int)
2> 参数为int, 任何实参均被提升成整型
3> 只能正确处理处于[0, 127]之间的值
 
功能二:字符映射
1> 函数原型为int toxxxx(int)
2> 对参数进行检测, 若符合范围则转换, 否则不变

备注说明:C语言下的此类功能头文件是<ctype.h>
以下是<cctype>头文件的声明:

// cctype standard header
#pragma once
#ifndef _CCTYPE_
#define _CCTYPE_
#include <yvals.h>

#ifdef _STD_USING
 #undef _STD_USING
  #include <ctype.h>
 #define _STD_USING
#else /* _STD_USING */
 #include <ctype.h>
#endif /* _STD_USING */
#ifndef RC_INVOKED
 #if _GLOBAL_USING
_STD_BEGIN
using ::isalnum; using ::isalpha; using ::iscntrl;
using ::isdigit; using ::isgraph; using ::islower;
using ::isprint; using ::ispunct; using ::isspace;
using ::isupper; using ::isxdigit; using ::tolower;
using ::toupper;

_STD_END
 #endif /* _GLOBAL_USING */
#endif /* RC_INVOKED */
	/* remove any (improper) macro overrides */
#undef isalnum
#undef isalpha
#undef isblank
#undef iscntrl
#undef isdigit
#undef isgraph
#undef islower
#undef isprint
#undef ispunct
#undef isspace
#undef isupper
#undef isxdigit
#undef tolower
#undef toupper
#endif /* _CCTYPE_ */
/*
 * Copyright (c) 1992-2006 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
 V5.02:0009 */


ASCII码字符检测结果表
下图为ASCII字符检测结果表格,画X的表示“是”(即真,返回值非0)

ASCII valuescharactersiscntrlisblankisspaceisupperislowerisalphaisdigitisxdigitisalnumispunctisgraphisprint
0x00 .. 0x08NUL, (other control codes)x










0x09tab ('\t')xxx








0x0A .. 0x0D(white-space control codes:'\f','\v','\n','\r')x
x








0x0E .. 0x1F(other control codes)x










0x20space (' ')
xx







x
0x21 .. 0x2F!"#$%&'()*+,-./








xxx
0x30 .. 0x390123456789





xxx
xx
0x3a .. 0x40:;<=>?@








xxx
0x41 .. 0x46ABCDEF


x
x
xx
xx
0x47 .. 0x5AGHIJKLMNOPQRSTUVWXYZ


x
x

x
xx
0x5B .. 0x60[\]^_`








xxx
0x61 .. 0x66abcdef



xx
xx
xx
0x67 .. 0x7Aghijklmnopqrstuvwxyz



xx

x
xx
0x7B .. 0x7E{|}~








xxx
0x7F(DEL)x









 


C++ reference上的:


header

<cctype> (ctype.h)

Character handling functions
This header declares a set of functions to classify and transform individual characters.

Functions

These functions take the  int equivalent of one character as parameter and return an  int that can either be another character or a value representing a boolean value: an  int value of  0 means false, and an  int value different from  0represents true.

There are two sets of functions:

Character classification functions
They check whether the character passed as parameter belongs to a certain category:


Character conversion functions
Two functions that convert between letter cases:

For the first set, here is a map of how the original 127-character ASCII set is considered by each function (an x indicates that the function returns true on that character)

ASCII valuescharactersiscntrlisblankisspaceisupperislowerisalphaisdigitisxdigitisalnumispunctisgraphisprint
0x00 .. 0x08NUL, (other control codes)x           
0x09tab ('\t')xxx         
0x0A .. 0x0D(white-space control codes:'\f','\v','\n','\r')x x         
0x0E .. 0x1F(other control codes)x           
0x20space (' ') xx        x
0x21 .. 0x2F!"#$%&'()*+,-./         xxx
0x30 .. 0x390123456789      xxx xx
0x3a .. 0x40:;<=>?@         xxx
0x41 .. 0x46ABCDEF   x x xx xx
0x47 .. 0x5AGHIJKLMNOPQRSTUVWXYZ   x x  x xx
0x5B .. 0x60[\]^_`         xxx
0x61 .. 0x66abcdef    xx xx xx
0x67 .. 0x7Aghijklmnopqrstuvwxyz    xx  x xx
0x7B .. 0x7E{|}~         xxx
0x7F(DEL)x           

The characters in the extended character set (above 0x7F) may belong to diverse categories depending on the locale and the platform. As a general rule,  ispunctisgraph and  isprint return true on these for the standard C locale on most platforms supporting extended character sets.


几个程序实例:

/*转自http://blog.csdn.net/lion_hbeu/article/details/23912551*/
#include <iostream>
#include <cctype>
#include <cstdio>
using namespace std;
void printHead();
int main()
{
    int choice;
    char str_ch[50];
    char str_ch1[50];
    cout<<"input your choice,(0:大小写转换测试,1:打印ASCII字符检测结果表)\n";
    cin>>choice;
    if(choice == 0)
    {
        cout<<"input a string\n";
        cin>>str_ch;
        cout<<"源字符串:"<<str_ch<<endl;
        int len = sizeof(str_ch);
        for(int i =0;i<len;i++)
            str_ch1[i] = tolower(str_ch[i]);
        cout<<"小写字符串:"<<str_ch1<<endl;
        for(int i =0;i<len;i++)
            str_ch1[i] =toupper(str_ch[i]);
        cout<<"大写字符串:"<<str_ch1<<endl;

    }
    else if(choice == 1)
    {
        printHead();
        for(int i=0;i<=127;i++)
        {
            printf("0x%02x%6d",i,i);
            if(i>=8&&i<=13 || i==127 )
            {
                printf("%10s","");
            }
            else
                printf("%10c",i);
            (isalnum(i)!=0)?printf("%10s","Y"):printf("%10s","N");
            (isalpha(i)!=0)?printf("%10s","Y"):printf("%10s","N");
            (isblank(i)!=0)?printf("%10s","Y"):printf("%10s","N");
            (iscntrl(i)!=0)?printf("%10s","Y"):printf("%10s","N");
            (isdigit(i)!=0)?printf("%10s","Y"):printf("%10s","N");
            (isgraph(i)!=0)?printf("%10s","Y"):printf("%10s","N");
            (islower(i)!=0)?printf("%10s","Y"):printf("%10s","N");
            (isprint(i)!=0)?printf("%10s","Y"):printf("%10s","N");
            (ispunct(i)!=0)?printf("%10s","Y"):printf("%10s","N");
            (isspace(i)!=0)?printf("%10s","Y"):printf("%10s","N");
            (isupper(i)!=0)?printf("%10s","Y"):printf("%10s","N");
            (isxdigit(i)!=0)?printf("%10s","Y"):printf("%10s","N");
            printf("\n");

        }
    }
    return 0;
}
void printHead()
{
    printf("%10s","value");
    printf("%10s","chara");
    printf("%10s","isalnum");
    printf("%10s","isalpha");
    printf("%10s","isblank");
    printf("%10s","iscntrl");
    printf("%10s","isdigit");
    printf("%10s","isgraph");
    printf("%10s","islower");
    printf("%10s","isprint");
    printf("%10s","ispunct");
    printf("%10s","isspace");
    printf("%10s","isupper");
    printf("%10s","isxdigit");
    printf("\n");
}

/*转自http://blog.csdn.net/gyysmall/article/details/12650937*/
//用循环判断ASCII从0~127之间的字符类型
/*
cctype中的常用函数
函数名称   返回值
isalnum()  如果参数是字母数字,即字母或者数字,函数返回true
isalpha()  如果参数是字母,函数返回true
iscntrl()  如果参数是控制字符,函数返回true
isdigit()  如果参数是数字(0-9),函数返回true
isgraph()  如果参数是除空格之外的打印字符,函数返回true
islower()  如果参数是小写字母,函数返回true
isprint()  如果参数是打印字符(包括空格),函数返回true
ispunct()  如果参数是标点符号,函数返回true
isspace()  如果参数是标准空白字符,如空格、换行符、水平或垂直制表符,函数返回true
isupper()  如果参数是大写字母,函数返回true
isxdigit() 如果参数是十六进制数字,即0-9、a-f、A-F,函数返回true
tolower()  如果参数是大写字符,返回其小写,否则返回该参数
toupper()  如果参数是小写字符,返回其大写,否则返回该参数
*/
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
	unsigned char c='\0';
	for(;(int)c<=127;c++)
	{
		cout<<"ASCII为 "<<(int)c<<" 的字符为 ";
		if(iscntrl(c))//判断是否为控制字符
		{	cout<<(iscntrl(c)?"控制字符  ":"");  
		    cout<<(isspace(c)?"标准空白字符,如空格、换行符、水平或垂直制表符":"");		
		}
		else  //非控制字符
		{			
            cout<<c<<" ";
			cout<<(isprint(c)?"打印字符(包括空格)  ":""); 
			cout<<(isgraph(c)?"除空格之外的打印字符  ":"");
			cout<<(isalnum(c)?"字母数字  ":"");
			cout<<(isalpha(c)?"字母  ":"");   
			cout<<(islower(c)?"小写字母  ":"");
			cout<<(isupper(c)?"大写字母  ":"");   
			cout<<(isdigit(c)?"数字(0-9)  ":"");
			cout<<(isxdigit(c)?"十六进制数字(0-9,A-F,a-f)  ":"");			  
			cout<<(ispunct(c)?"标点符号  ":"");
		}
		cout<<endl;
		//cout<<"============================================="<<endl; 
	}
	return 0;
}


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值