Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Arduino character function


May 15, 2021 Arduino


Table of contents


All data is entered into the computer in characters, including letters, numbers, and various special symbols. /b10> In this section, we discuss the ability to examine and manipulate individual characters in C.

The character processing library consists of several functions that perform useful tests and character data operations. /b10> Each function receives a character, represented as int or EOF as an argument. /b11> Characters are usually operated as integers.

Keep in mind that EOF usually has a value of -1, and some hardware architectures do not allow negative values to be stored in the char variable. /b10> Therefore, the character handler manipulates characters as integers.

The following table summarizes the functions of the character processing library. /b10> When working with functions in the library with characters, include the title of the .

Serial number Prototype and description
1

int isdigit(int c)

If c is a number, 1 is returned, otherwise 0 is returned.

2

int isalpha(int c)

If c is the letter, 1 is returned, otherwise 0 is returned.

3

int isalnum(int c)

If c is a number or letter, 1 is returned, otherwise 0 is returned.

4

int isxdigit(int c)

If c is a hetethic numeric character, 1 is returned, otherwise 0 is returned.

5

int islower(int c)

If c is lowercase, return 1 or 0.

6

int isupper(int c)

If c is capital letters, 1 is returned;

7

int isspace(int c)

If c is a blank character: line breaks (''n'), space characters ('),' page breaks ('f'), carriage returns ('r'), horizontal tabs (''t'), or vertical tabs (''v'),' return 1, otherwise 0 is returned.

8

int iscntrl(int c)

If c is a control character, such as a line break( ''n'), a page break (''f'), a carriage return character ('),' a horizontal tab ('v'), a vertical tab (''v'), an alert (' or a ''b'), then 1 is returned, otherwise 0 is returned.

9

int ispunct(int c)

If c is a printed character other than a space, number, or letter, 1 is returned, otherwise 0 is returned.

10

int isprint(int c)

If c is a printed character that contains a space (''), 1 is returned, otherwise 0 is returned.

11

int isgraph(int c)

If c is a printed character other than a space (''), 1 is returned, otherwise 0 is returned.

Example

The following example shows how to use the functions isdigit, isalpha, isalnum, and isxdigit. /b10> The function isdigit determines whether its arguments are numbers (0-9). /b11> The function isalpha determines whether its arguments are capital letters (A-Z) or lowercase letters (a-z). /b12> The function isalnum determines whether its arguments are capital, lowercase, or numeric. /b13> The function isxdigit determines whether its arguments are hex digits (A-F, a-f, 0-9).

Example 1

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 () {

}

Results

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

We use conditional operators (?:) for each function to determine whether the string "is a" or the string "is not a" should be printed in the output of each test character. F or example, line a indicates that if "8" is a number, that is, if isdigit returns a true (non-zero) value, the string "8 is a" is printed. If "8" is not a number (that is, if isdigit returns 0), the string "8 is not a" is printed.

Example 2

The following example demonstrates the use of functions islower and isupper. /b10> The function islower determines whether its arguments are lowercase (a-z). /b11> The function isupper determines whether its arguments are capital letters (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 () {

}

Results

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

Example 3

The following example shows how to use functions isspace, iscntrl, ispunct, isprint, and isgraph.

  • The function isspace determines whether its arguments are blank characters, such as spaces ('), page breaks (''f'), line breaks ('), carriage returns ('), horizontal tabs (' or vertical tabs ').

  • The function iscntrl determines whether its parameters are control characters, such as horizontal tabs (''t'), vertical tabs (''v'), page breaks (''f'), alerts (''a'), back-character characters ('b'), carriage return characters (' or line breaks').

  • The function ispunct determines whether its parameters are printed characters other than spaces, numbers, or letters (e.g., $, , ( , , , , ,: or%).

  • The function isprint determines whether its parameters are characters that can be displayed on the screen, including space characters.

  • The function isgraph tests the same characters as the isprint, but does not include space characters.

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 () {

}

Results

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