📅  最后修改于: 2020-11-05 03:33:16             🧑  作者: Mango
所有数据都以字符形式输入计算机,其中包括字母,数字和各种特殊符号。在本节中,我们讨论C++用于检查和操作单个字符。
字符处理库包括几个函数,这些函数可以执行有用的字符数据测试和操作。每个函数接收一个字符(表示为int或EOF作为参数)。字符通常作为整数操作。
请记住,EOF通常具有值–1,并且某些硬件体系结构不允许将负值存储在char变量中。因此,文字处理功能操作字符为整数。
下表总结了字符处理库的功能。使用字符处理库中的函数时,请包含
S.No. | Prototype & Description |
---|---|
1 |
int isdigit( int c ) Returns 1 if c is a digit and 0 otherwise. |
2 |
int isalpha( int c ) Returns 1 if c is a letter and 0 otherwise. |
3 |
int isalnum( int c ) Returns 1 if c is a digit or a letter and 0 otherwise. |
4 |
int isxdigit( int c ) Returns 1 if c is a hexadecimal digit character and 0 otherwise. (See Appendix D, Number Systems, for a detailed explanation of binary, octal, decimal and hexadecimal numbers.) |
5 |
int islower( int c ) Returns 1 if c is a lowercase letter and 0 otherwise. |
6 |
int isupper( int c ) Returns 1 if c is an uppercase letter; 0 otherwise. |
7 |
int isspace( int c ) Returns 1 if c is a white-space character—newline (‘\n’), space (‘ ‘), form feed (‘\f’), carriage return (‘\r’), horizontal tab (‘\t’), or vertical tab (‘\v’)—and 0 otherwise. |
8 |
int iscntrl( int c ) Returns 1 if c is a control character, such as newline (‘\n’), form feed (‘\f’), carriage return (‘\r’), horizontal tab (‘\t’), vertical tab (‘\v’), alert (‘\a’), or backspace (‘\b’)—and 0 otherwise. |
9 |
int ispunct( int c ) Returns 1 if c is a printing character other than a space, a digit, or a letter and 0 otherwise. |
10 |
int isprint( int c ) Returns 1 if c is a printing character including space (‘ ‘) and 0 otherwise. |
11 |
int isgraph( int c ) Returns 1 if c is a printing character other than space (‘ ‘) and 0 otherwise. |
下面的示例演示函数isdigit,isalpha,isalnum和isxdigit的用法。函数isdigit确定其参数是否为数字(0–9)。 isalpha函数确定其自变量是大写字母(AZ)还是小写字母(a–z)。 isalnum函数确定其参数是大写,小写字母还是数字。函数isxdigit确定其参数是否为十六进制数字(AF,af,0-9)。
void setup () {
Serial.begin (9600);
Serial.print ("According to isdigit:\r");
Serial.print (isdigit( '8' ) ? "8 is a": "8 is not a");
Serial.print (" digit\r" );
Serial.print (isdigit( '8' ) ?"# is a": "# is not a") ;
Serial.print (" digit\r");
Serial.print ("\rAccording to isalpha:\r" );
Serial.print (isalpha('A' ) ?"A is a": "A is not a");
Serial.print (" letter\r");
Serial.print (isalpha('A' ) ?"b is a": "b is not a");
Serial.print (" letter\r");
Serial.print (isalpha('A') ?"& is a": "& is not a");
Serial.print (" letter\r");
Serial.print (isalpha( 'A' ) ?"4 is a":"4 is not a");
Serial.print (" letter\r");
Serial.print ("\rAccording to isalnum:\r");
Serial.print (isalnum( 'A' ) ?"A is a" : "A is not a" );
Serial.print (" digit or a letter\r" );
Serial.print (isalnum( '8' ) ?"8 is a" : "8 is not a" ) ;
Serial.print (" digit or a letter\r");
Serial.print (isalnum( '#' ) ?"# is a" : "# is not a" );
Serial.print (" digit or a letter\r");
Serial.print ("\rAccording to isxdigit:\r");
Serial.print (isxdigit( 'F' ) ?"F is a" : "F is not a" );
Serial.print (" hexadecimal digit\r" );
Serial.print (isxdigit( 'J' ) ?"J is a" : "J is not a" ) ;
Serial.print (" hexadecimal digit\r" );
Serial.print (isxdigit( '7' ) ?"7 is a" : "7 is not a" ) ;
Serial.print (" hexadecimal digit\r" );
Serial.print (isxdigit( '$' ) ? "$ is a" : "$ is not a" );
Serial.print (" hexadecimal digit\r" );
Serial.print (isxdigit( 'f' ) ? “f is a" : "f is not a");
}
void loop () {
}
According to isdigit:
8 is a digit
# is not a digit
According to isalpha:
A is a letter
b is a letter
& is not a letter
4 is not a letter
According to isalnum:
A is a digit or a letter
8 is a digit or a letter
# is not a digit or a letter
According to isxdigit:
F is a hexadecimal digit
J is not a hexadecimal digit
7 is a hexadecimal digit
$ is not a hexadecimal digit
f is a hexadecimal digit
我们在每个函数使用条件运算符(?:)来确定应该在输出中为每个测试的字符打印字符串“是”还是字符串“不是”。例如,行a表示如果’8’是一个数字(即,如果isdigit返回一个真(非零)值),则打印字符串“ 8 is a”。如果“ 8”不是数字(即,如果isdigit返回0),则打印字符串“ 8不是数字”。
下面的示例演示islower和isupper函数的用法。 islower函数确定其参数是否为小写字母(a–z)。函数isupper确定其参数是否为大写字母(A–Z)。
int thisChar = 0xA0;
void setup () {
Serial.begin (9600);
Serial.print ("According to islower:\r") ;
Serial.print (islower( 'p' ) ? "p is a" : "p is not a" );
Serial.print ( " lowercase letter\r" );
Serial.print ( islower( 'P') ? "P is a" : "P is not a") ;
Serial.print ("lowercase letter\r");
Serial.print (islower( '5' ) ? "5 is a" : "5 is not a" );
Serial.print ( " lowercase letter\r" );
Serial.print ( islower( '!' )? "! is a" : "! is not a") ;
Serial.print ("lowercase letter\r");
Serial.print ("\rAccording to isupper:\r") ;
Serial.print (isupper ( 'D' ) ? "D is a" : "D is not an" );
Serial.print ( " uppercase letter\r" );
Serial.print ( isupper ( 'd' )? "d is a" : "d is not an") ;
Serial.print ( " uppercase letter\r" );
Serial.print (isupper ( '8' ) ? "8 is a" : "8 is not an" );
Serial.print ( " uppercase letter\r" );
Serial.print ( islower( '$' )? "$ is a" : "$ is not an") ;
Serial.print ("uppercase letter\r ");
}
void setup () {
}
According to islower:
p is a lowercase letter
P is not a lowercase letter
5 is not a lowercase letter
! is not a lowercase letter
According to isupper:
D is an uppercase letter
d is not an uppercase letter
8 is not an uppercase letter
$ is not an uppercase letter
以下示例演示了函数isspace,iscntrl,ispunct,isprint和isgraph的用法。
函数isspace决定其参数是否为空格字符,例如空格(”),换页(’\ f’),换行符(’\ n’),回车符(’\ r’),水平制表符(’\ t’)或垂直标签(’\ v’)。
函数iscntrl判断确定其参数是否是一个控制字符,如水平制表( ‘\ T’),垂直制表符( ‘\ V’),形式进料( ‘\ F’),警报( ‘\ A’),退格( ‘\ b’),回车符(’\ r’)或换行符(’\ n’)。
函数ispunct确定其参数是否为空格,数字或字母以外的其他印刷字符,例如$,#,(,),[,],{,},;,:或%。
isprint函数确定其参数是否为可以在屏幕上显示的字符(包括空格字符)。
isgraph函数会测试与isprint相同的字符,但不包括空格字符。
void setup () {
Serial.begin (9600);
Serial.print ( " According to isspace:\rNewline ") ;
Serial.print (isspace( '\n' )? " is a" : " is not a" );
Serial.print ( " whitespace character\rHorizontal tab") ;
Serial.print (isspace( '\t' )? " is a" : " is not a" );
Serial.print ( " whitespace character\n") ;
Serial.print (isspace('%')? " % is a" : " % is not a" );
Serial.print ( " \rAccording to iscntrl:\rNewline") ;
Serial.print ( iscntrl( '\n' )?"is a" : " is not a" ) ;
Serial.print (" control character\r");
Serial.print (iscntrl( '$' ) ? " $ is a" : " $ is not a" );
Serial.print (" control character\r");
Serial.print ("\rAccording to ispunct:\r");
Serial.print (ispunct(';' ) ?"; is a" : "; is not a" ) ;
Serial.print (" punctuation character\r");
Serial.print (ispunct('Y' ) ?"Y is a" : "Y is not a" ) ;
Serial.print ("punctuation character\r");
Serial.print (ispunct('#' ) ?"# is a" : "# is not a" ) ;
Serial.print ("punctuation character\r");
Serial.print ( "\r According to isprint:\r");
Serial.print (isprint('$' ) ?"$ is a" : "$ is not a" );
Serial.print (" printing character\rAlert ");
Serial.print (isprint('\a' ) ?" is a" : " is not a" );
Serial.print (" printing character\rSpace ");
Serial.print (isprint(' ' ) ?" is a" : " is not a" );
Serial.print (" printing character\r");
Serial.print ("\r According to isgraph:\r");
Serial.print (isgraph ('Q' ) ?"Q is a" : "Q is not a" );
Serial.print ("printing character other than a space\rSpace ");
Serial.print (isgraph (' ') ?" is a" : " is not a" );
Serial.print ("printing character other than a space ");
}
void loop () {
}
According to isspace:
Newline is a whitespace character
Horizontal tab is a whitespace character
% is not a whitespace character
According to iscntrl:
Newline is a control character
$ is not a control character
According to ispunct:
; is a punctuation character
Y is not a punctuation character
# is a punctuation character
According to isprint:
$ is a printing character
Alert is not a printing character
Space is a printing character
According to isgraph:
Q is a printing character other than a space
Space is not a printing character other than a space