课程表

Arduino 基础

Arduino 函数库

Arduino 进阶

Arduino 项目

Arduino 传感器

Arduino 电机控制

Arduino 声音

工具箱
速查手册

Arduino 字符函数

当前位置:免费教程 » 程序设计 » Arduino

所有数据作为字符输入计算机,包括字母,数字和各种特殊符号。 在本节中,我们讨论C ++用于检查和处理单个字符的能力。


字符处理库包括执行字符数据的有用测试和操作的几个函数。 每个函数接收一个字符,表示为int或EOF作为参数。 字符通常作为整数操作。


记住,EOF通常具有值-1,并且一些硬件架构不允许负值存储在char变量中。 因此,字符处理函数将字符操作为整数。


下表总结了字符处理库的功能。 使用字符处理库中的函数时,请包含< cctype> 标题。

S.No。原型和描述
1

int isdigit(int c)

如果c是数字,则返回1,否则返回0。

2

int isalpha(int c)

如果c是字母,则返回1,否则返回0。

3

int isalnum(int c)

如果c是数字或字母,则返回1,否则返回0。

4

int isxdigit(int c)

如果c是十六进制数字字符,则返回1,否则返回0。

(有关二进制,八进制,十进制和十六进制数字的详细说明,请参见附录D,数字系统。)

5

int islower(int c)

如果c是小写字母,则返回1,否则返回0。

6

int isupper(int c)

如果c是大写字母,则返回1; 0否则。

7

int isspace(int c)

如果c是空格字符换行符('\ n'),空格,则返回1
('\ f'),回车符('\ r'),水平制表符('\ t')或垂直制表符('\ v'),否则为0。

8

int iscntrl(int c)

如果c是控制字符,返回1,如换行符('\ n'),换页符('\ f'),回车符('\ r'),水平制表符 \ v'),alert('\ a')或退格('\ b') - 否则为0。

9

int ispunct(int c)

如果c是除空格,数字或字母以外的打印字符,则返回1,否则返回0。

10

int isprint(int c)

如果c是包含空格('')的打印字符,则返回1,否则返回0。

11

int isgraph(int c)

如果c是除空格('')之外的打印字符,则返回1,否则返回0。


例子

以下示例演示如何使用函数 isdigit,isalpha,isalnum isxdigit 函数 isdigit 确定其参数是否为数字(0-9)。 函数 isalpha 确定其参数是大写字母(A-Z)还是小写字母(a-z)。 函数 isalnum 确定其参数是大写,小写字母还是数字。 函数 isxdigit 确定其参数是否为十六进制数字(A-F,a-f,0-9)。


实例1

  1. void setup () {
  2. Serial.begin (9600);
  3. Serial.print ("According to isdigit:\r");
  4. Serial.print (isdigit( '8' ) ? "8 is a": "8 is not a");
  5. Serial.print (" digit\r" );
  6. Serial.print (isdigit( '8' ) ?"# is a": "# is not a") ;
  7. Serial.print (" digit\r");
  8. Serial.print ("\rAccording to isalpha:\r" );
  9. Serial.print (isalpha('A' ) ?"A is a": "A is not a");
  10. Serial.print (" letter\r");
  11. Serial.print (isalpha('A' ) ?"b is a": "b is not a");
  12. Serial.print (" letter\r");
  13. Serial.print (isalpha('A') ?"& is a": "& is not a");
  14. Serial.print (" letter\r");
  15. Serial.print (isalpha( 'A' ) ?"4 is a":"4 is not a");
  16. Serial.print (" letter\r");
  17. Serial.print ("\rAccording to isalnum:\r");
  18. Serial.print (isalnum( 'A' ) ?"A is a" : "A is not a" );
  19.  
  20. Serial.print (" digit or a letter\r" );
  21. Serial.print (isalnum( '8' ) ?"8 is a" : "8 is not a" ) ;
  22. Serial.print (" digit or a letter\r");
  23. Serial.print (isalnum( '#' ) ?"# is a" : "# is not a" );
  24. Serial.print (" digit or a letter\r");
  25. Serial.print ("\rAccording to isxdigit:\r");
  26. Serial.print (isxdigit( 'F' ) ?"F is a" : "F is not a" );
  27. Serial.print (" hexadecimal digit\r" );
  28. Serial.print (isxdigit( 'J' ) ?"J is a" : "J is not a" ) ;
  29. Serial.print (" hexadecimal digit\r" );
  30. Serial.print (isxdigit( '7' ) ?"7 is a" : "7 is not a" ) ;
  31.  
  32. Serial.print (" hexadecimal digit\r" );
  33. Serial.print (isxdigit( '$' ) ? "$ is a" : "$ is not a" );
  34. Serial.print (" hexadecimal digit\r" );
  35. Serial.print (isxdigit( 'f' ) ? f is a" : "f is not a");
  36. }
  37.  
  38. void loop () {
  39.  
  40. }


结果

  1. According to isdigit:
  2. 8 is a digit
  3. # is not a digit
  4. According to isalpha:
  5. A is a letter
  6. b is a letter
  7. & is not a letter
  8. 4 is not a letter
  9. According to isalnum:
  10. A is a digit or a letter
  11.  
  12. 8 is a digit or a letter
  13. # is not a digit or a letter
  14. According to isxdigit:
  15. F is a hexadecimal digit
  16. J is not a hexadecimal digit
  17. 7 is a hexadecimal digit
  18.  
  19. $ is not a hexadecimal digit
  20. f is a hexadecimal digit

我们使用每个函数的条件运算符(?:)来确定字符串“is a”或字符串“is not a”是否应该在每个测试字符的输出中打印。 例如,行a表示如果'8'是数字,即如果isdigit返回真(非零)值,则打印字符串“8是a”。 如果'8'不是数字(即,如果isdigit返回0),则打印字符串“8不是a”。


实例2

以下示例演示了函数 islower isupper 的使用。 函数 islower 确定其参数是否为小写字母(a-z)。 函数 isupper 确定其参数是否为大写字母(A-Z)。

  1. int thisChar = 0xA0;
  2.  
  3. void setup () {
  4. Serial.begin (9600);
  5. Serial.print ("According to islower:\r") ;
  6. Serial.print (islower( 'p' ) ? "p is a" : "p is not a" );
  7. Serial.print ( " lowercase letter\r" );
  8. Serial.print ( islower( 'P') ? "P is a" : "P is not a") ;
  9. Serial.print ("lowercase letter\r");
  10. Serial.print (islower( '5' ) ? "5 is a" : "5 is not a" );
  11. Serial.print ( " lowercase letter\r" );
  12. Serial.print ( islower( '!' )? "! is a" : "! is not a") ;
  13. Serial.print ("lowercase letter\r");
  14.  
  15. Serial.print ("\rAccording to isupper:\r") ;
  16. Serial.print (isupper ( 'D' ) ? "D is a" : "D is not an" );
  17. Serial.print ( " uppercase letter\r" );
  18. Serial.print ( isupper ( 'd' )? "d is a" : "d is not an") ;
  19. Serial.print ( " uppercase letter\r" );
  20. Serial.print (isupper ( '8' ) ? "8 is a" : "8 is not an" );
  21. Serial.print ( " uppercase letter\r" );
  22. Serial.print ( islower( '$' )? "$ is a" : "$ is not an") ;
  23. Serial.print ("uppercase letter\r ");
  24. }
  25.  
  26. void setup () {
  27.  
  28. }


结果

  1. According to islower:
  2. p is a lowercase letter
  3. P is not a lowercase letter
  4. 5 is not a lowercase letter
  5. ! is not a lowercase letter
  6.  
  7. According to isupper:
  8. D is an uppercase letter
  9. d is not an uppercase letter
  10. 8 is not an uppercase letter
  11. $ is not an uppercase letter


实例3

以下示例演示如何使用函数 isspace,iscntrl,ispunct,isprint isgraph

  • 函数 isspace 确定其参数是否为空格字符,例如空格(''),换页符('\ f'),换行符('\ n'),回车符('\ r' ('\ t')或垂直制表符('\ v')。

  • 函数 iscntrl 确定其参数是否为控制字符,如水平制表符('\ t'),垂直制表符('\ v'),表单feed('\ f'),alert('\ a'),backspace '\ b'),回车('\ r')或换行符('\ n')。

  • 函数 ispunct 确定其参数是否是除空格,数字或字母(例如$,#,(,),[,],{,},;,,或%)以外的打印字符。

  • 函数 isprint 确定其参数是否为可以在屏幕上显示的字符(包括空格字符)。

  • 函数 isgraph 测试与isprint相同的字符,但不包括空格字符。

  1. void setup () {
  2. Serial.begin (9600);
  3. Serial.print ( " According to isspace:\rNewline ") ;
  4. Serial.print (isspace( '\n' )? " is a" : " is not a" );
  5. Serial.print ( " whitespace character\rHorizontal tab") ;
  6. Serial.print (isspace( '\t' )? " is a" : " is not a" );
  7. Serial.print ( " whitespace character\n") ;
  8. Serial.print (isspace('%')? " % is a" : " % is not a" );
  9. Serial.print ( " \rAccording to iscntrl:\rNewline") ;
  10. Serial.print ( iscntrl( '\n' )?"is a" : " is not a" ) ;
  11. Serial.print (" control character\r");
  12. Serial.print (iscntrl( '$' ) ? " $ is a" : " $ is not a" );
  13. Serial.print (" control character\r");
  14. Serial.print ("\rAccording to ispunct:\r");
  15. Serial.print (ispunct(';' ) ?"; is a" : "; is not a" ) ;
  16. Serial.print (" punctuation character\r");
  17. Serial.print (ispunct('Y' ) ?"Y is a" : "Y is not a" ) ;
  18. Serial.print ("punctuation character\r");
  19. Serial.print (ispunct('#' ) ?"# is a" : "# is not a" ) ;
  20. Serial.print ("punctuation character\r");
  21.  
  22. Serial.print ( "\r According to isprint:\r");
  23. Serial.print (isprint('$' ) ?"$ is a" : "$ is not a" );
  24. Serial.print (" printing character\rAlert ");
  25. Serial.print (isprint('\a' ) ?" is a" : " is not a" );
  26. Serial.print (" printing character\rSpace ");
  27. Serial.print (isprint(' ' ) ?" is a" : " is not a" );
  28. Serial.print (" printing character\r");
  29. Serial.print ("\r According to isgraph:\r");
  30. Serial.print (isgraph ('Q' ) ?"Q is a" : "Q is not a" );
  31. Serial.print ("printing character other than a space\rSpace ");
  32. Serial.print (isgraph (' ') ?" is a" : " is not a" );
  33. Serial.print ("printing character other than a space ");
  34. }
  35.  
  36. void loop () {
  37.  
  38. }


结果

  1. According to isspace:
  2. Newline is a whitespace character
  3. Horizontal tab is a whitespace character
  4. % is not a whitespace character
  5. According to iscntrl:
  6. Newline is a control character
  7. $ is not a control character
  8. According to ispunct:
  9. ; is a punctuation character
  10. Y is not a punctuation character
  11. # is a punctuation character
  12. According to isprint:
  13. $ is a printing character
  14. Alert is not a printing character
  15. Space is a printing character
  16. According to isgraph:
  17. Q is a printing character other than a space
  18. Space is not a printing character other than a space
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号