所有數(shù)據(jù)都以字符形式輸入計算機,包括字母,數(shù)字和各種特殊符號。在本章節(jié)中,我們討論C++檢查和操作單個字符的功能。
字符處理庫包括幾個函數(shù),執(zhí)行有用的測試和字符數(shù)據(jù)的操作。每個函數(shù)接收一個字符,表示為int或EOF作為參數(shù)。字符通常作為整數(shù)操作。
記住,EOF通常具有值-1,而一些硬件架構(gòu)不允許負(fù)值存儲在char變量中。因此,字符處理函數(shù)將字符作為整數(shù)來操作。
下表總結(jié)了字符處理庫的函數(shù)。使用字符處理庫中的函數(shù)時,請包含<cctype>標(biāo)題。
序號 | 原型和描述 |
---|---|
1 | int isdigit(int c) 如果c是數(shù)字,則返回1,否則返回0。 |
2 | int isalpha(int c) 如果c是字母,則返回1,否則返回0。 |
3 | int isalnum(int c) 如果c是數(shù)字或字母,則返回1,否則返回0。 |
4 | int isxdigit(int c) 如果c是十六進制數(shù)字字符,則返回1,否則返回0。 |
5 | int islower(int c) 如果c是小寫字母,則返回1,否則返回0。 |
6 | int isupper(int c) 如果c是大寫字母,則返回1;否則返回0。 |
7 | int isspace(int c) 如果c是空白字符:換行符('\n')、空格符(' ')、換頁符('\f')、回車符('\r')、水平制表符('\t')或垂直制表符('\v'),則返回1,否則返回0。 |
8 | int iscntrl(int c) 如果c是控制字符,如換行符('\n')、換頁符('\f')、回車符('\r')、水平制表符 ('\t')、垂直制表符('\v')、alert('\a')或退格('\b'),則返回1,否則返回0。 |
9 | int ispunct(int c) 如果c是除空格,數(shù)字或字母以外的打印字符,則返回1,否則返回0。 |
10 | int isprint(int c) 如果c是包含空格(' ')的打印字符,則返回1,否則返回0。 |
11 | int isgraph(int c) 如果c是除空格(' ')之外的打印字符,則返回1,否則返回0。 |
以下示例演示如何使用函數(shù) isdigit,isalpha,isalnum 和 isxdigit 。函數(shù) isdigit 確定其參數(shù)是否為數(shù)字(0-9)。函數(shù) isalpha 確定其參數(shù)是大寫字母(A-Z)還是小寫字母(a-z)。函數(shù) isalnum 確定其參數(shù)是大寫,小寫字母還是數(shù)字。函數(shù) isxdigit 確定其參數(shù)是否為十六進制數(shù)字(A-F,a-f,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
我們對每個函數(shù)使用條件運算符(?:)來確定字符串“is a”或字符串“is not a”是否應(yīng)該打印在每個測試字符的輸出中。例如,行a表示如果“8”是數(shù)字,即如果isdigit返回真(非零)值,則打印字符串“8 is a”。如果“8”不是數(shù)字(即,如果isdigit返回0),則打印字符串“8 is not a”。
以下示例演示了函數(shù) islower 和 isupper 的使用。函數(shù) islower 確定其參數(shù)是否為小寫字母(a-z)。函數(shù) isupper 確定其參數(shù)是否為大寫字母(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
以下示例演示如何使用函數(shù) isspace,iscntrl,ispunct,isprint 和 isgraph 。
函數(shù) isspace 確定其參數(shù)是否為空白字符,例如空格(' '),換頁符('\f'),換行符('\n'),回車符('\r'),水平制表符('\t')或垂直制表符('\v')。
函數(shù) iscntrl 確定其參數(shù)是否為控制字符,如水平制表符('\t'),垂直制表符('\v'),換頁符('\f'),alert('\a'),退格符('\b'),回車符('\r')或換行符('\n')。
函數(shù) ispunct 確定其參數(shù)是否是除空格,數(shù)字或字母以外的打印字符(例如$,#,(,),[,],{,},;,:或%)。
函數(shù) isprint 確定其參數(shù)是否為可以在屏幕上顯示的字符(包括空格字符)。
函數(shù) 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
更多建議: