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 values | characters | iscntrl | isblank | isspace | isupper | islower | isalpha | isdigit | isxdigit | isalnum | ispunct | isgraph | isprint |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0x00 .. 0x08 | NUL, (other control codes) | x | |||||||||||
0x09 | tab ('\t') | x | x | x | |||||||||
0x0A .. 0x0D | (white-space control codes:'\f','\v','\n','\r') | x | x | ||||||||||
0x0E .. 0x1F | (other control codes) | x | |||||||||||
0x20 | space (' ') | x | x | x | |||||||||
0x21 .. 0x2F | !"#$%&'()*+,-./ | x | x | x | |||||||||
0x30 .. 0x39 | 0123456789 | 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 | |
C++ reference上的:
<cctype> (ctype.h)
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:-
isalnum
- Check if character is alphanumeric (function )
-
isalpha
- Check if character is alphabetic (function )
-
isblank
- Check if character is blank (function )
-
iscntrl
- Check if character is a control character (function )
-
isdigit
- Check if character is decimal digit (function )
-
isgraph
- Check if character has graphical representation (function )
-
islower
- Check if character is lowercase letter (function )
-
isprint
- Check if character is printable (function )
-
ispunct
- Check if character is a punctuation character (function )
-
isspace
- Check if character is a white-space (function )
-
isupper
- Check if character is uppercase letter (function )
-
isxdigit
- Check if character is hexadecimal digit (function )
Character conversion functions
Two functions that convert between letter cases:-
tolower
- Convert uppercase letter to lowercase (function )
-
toupper
- Convert lowercase letter to uppercase (function )
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 values | characters | iscntrl | isblank | isspace | isupper | islower | isalpha | isdigit | isxdigit | isalnum | ispunct | isgraph | isprint |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0x00 .. 0x08 | NUL, (other control codes) | x | |||||||||||
0x09 | tab ('\t') | x | x | x | |||||||||
0x0A .. 0x0D | (white-space control codes:'\f','\v','\n','\r') | x | x | ||||||||||
0x0E .. 0x1F | (other control codes) | x | |||||||||||
0x20 | space (' ') | x | x | x | |||||||||
0x21 .. 0x2F | !"#$%&'()*+,-./ | x | x | x | |||||||||
0x30 .. 0x39 | 0123456789 | 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 |
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, ispunct, isgraph 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;
}