Character functions in Arduino
As there are different types of characters used in Arduino so for each type there is a character function to recognize that type of character.The following are the eleven types of character functions that are briefly explained in this discourse:
- isuppercase() and islowercase() character functions
- isdigit() character function
- isspace() character function
- ispunct() character function
- isxdigit() character function
- isalpha() character function
- isascii() character function
- iscntrl() character function
- isprint() character function
- Isalnum() character function
- isGraph() character function
The explanation of each function is supported by its respective example.The examples of the Arduino program given are using the ternary operator. This operator is further divided into three parts. The first part is the condition which is to be checked and then comes the operator and if the result is correct then the value given first is displayed and if not the second value will be displayed. This can be further cleared by the syntax of the ternary operator:
isupperCase() and islowerCase() character functions
The upper-case function is used to check if the character used in Arduino programming is in capital letters or not. Similarly, to determine if the character is written in small letters, then the function lower case is used. The example code for both the functions is given:
Serial.begin (9600);
Serial.print (" Function islower says:\r") ;
Serial.print (islower( 'F' ) ? "f is " : "F is not " );
Serial.println ( " lowercase letter\r" );
Serial.print ("Function islower says :\r") ;
Serial.print (islower( 'f' ) ? "f is a" : "F is not a" );
Serial.println ( " lowercase letter\r" );
Serial.print ("Function isupper says:\r") ;
Serial.print (isupper( 'F' ) ? "F is " : "f is not " );
Serial.println ( " uppercase letter\r" );
Serial.print ("Function isupper says :\r") ;
Serial.print (isupper( 'f' ) ? "F is a" : "f is not a" );
Serial.println ( " uppercase letter\r" );
}
void loop () {
}
Output
isdigit() character function
To check whether the character is a number or a letter the isdigit() function is used in Arduino programming. The example code gives further clear understanding of the function.
Serial.begin (9600);
Serial.print (" Function isdigit says:\r") ;
Serial.print (isdigit( '1' ) ? "1 is " : "1 is not " );
Serial.println ( " Digit \r" );
Serial.print ("Function isdigit says :\r") ;
Serial.print (isdigit( 'a' ) ? "a is a" : "a is not a" );
Serial.println ( " Digit\r" );
}
void loop () {
}
Output
isspace() character function
To determine if there is a space used in the Arduino program the character function isspace() is used. The spaces include tab space both horizontal or vertical, space for new line, form feed space , return space. Here in the example code all types of spaces are used and determined by using the isspace() function. The output of the Arduino program is in binary form. If the output is one then it means true and if the result is zero then it means false.
char character2 = '\t';
char character3 = 'C';
char character4 = '\n';
char character5 = '\f';
char character6= 'a';
char character7='\v';
char character8= '\r';
void setup() {// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(isSpace(character1));
Serial.println(isSpace(character2));
Serial.println(isSpace(character3));
Serial.println(isSpace(character4));
Serial.println(isSpace(character5));
Serial.println(isSpace(character6));
Serial.println(isSpace(character7));
Serial.println(isSpace(character8));
}
void loop() {
}
Output
ispunct() character function
To identify any punctuation marks in the code the function ispunct() is used. In the example four punctuation marks are used and one character and one number is also used to give a clear picture of the working of the function.
Serial.begin (9600);
Serial.print ("function ispunct says:\r");
Serial.print (ispunct( '!' ) ? "! is a": "! is not a");
Serial.println(" punctuation mark \r" );
Serial.print ("function ispunct says:\r");
Serial.print (ispunct( ':' ) ? ": is a": ": is not a");
Serial.println(" punctuation mark \r" );
Serial.print ("function ispunct says:\r");
Serial.print (ispunct( '0' ) ? "0 is a": "0 is not a");
Serial.println(" punctuation mark \r" );
Serial.print ("function ispunct says:\r");
Serial.print (ispunct( 'a' ) ? "a is a": "a is not a");
Serial.println(" punctuation mark \r" );
Serial.print ("function ispunct says:\r");
Serial.print (ispunct( ';' ) ? "; is a": "; is not a");
Serial.println(" punctuation mark \r" );
Serial.print ("function ispunct says:\r");
Serial.print (ispunct( '.' ) ? ". is a": ". is not a");
Serial.println(" punctuation mark \r" );
}
void loop(){
}
Output
isxdigit() character function
In Arduino programming to pick out the hexadecimal numbers the isxdigit() character function is used. The hexadecimal contains symbols from 0 to 9 and some other alphabetic symbols from A to F. Further it can be cleared from the example code:
Serial.begin (9600);
Serial.print ("function isxdigit says:\r");
Serial.print (isxdigit( 'a' ) ? "a is a": "a is not a");
Serial.println(" hexadecimal digit \r" );
Serial.print ("function isxdigit says:\r");
Serial.print (isxdigit( '10' ) ? "10 is a": "10 is not a");
Serial.println(" hexadecimal digit \r" );
Serial.print ("function isxdigit says:\r");
Serial.print (isxdigit( 'e' ) ? "e is a": "e is not a");
Serial.println(" hexadecimal digit \r" );
Serial.print ("function isxdigit says:\r");
Serial.print (isxdigit( 'y' ) ? "y is a": "y is not a");
Serial.println(" hexadecimal digit \r" );
Serial.print ("function isxdigit says:\r");
Serial.print (isxdigit( '2' ) ? "2 is a": "2 is not a");
Serial.println(" hexadecimal digit \r" );
}
void loop(){
}
Output
isalpha() character function
The isalpha() function is used to identify the letters in Arduino programming. It checks whether a character has a letter or number.
Serial.begin (9600);
Serial.print ("function isalpha says:\r");
Serial.print (isalpha( 'a' ) ? "a is a": "a is not a");
Serial.println(" letter \r" );
Serial.print ("function isalpha says:\r");
Serial.print (isalpha( '10' ) ? "10 is a": "10 is not a");
Serial.println(" letter \r" );
Serial.print ("function isalpha says:\r");
Serial.print (isalpha( 'e' ) ? "e is a": "e is not a");
Serial.println(" letter \r" );
Serial.print ("function isalpha says:\r");
Serial.print (isalpha( '#' ) ? "# is a": "# is not a");
Serial.println(" letter \r" );
Serial.print ("function isalpha says:\r");
Serial.print (isalpha( 'D' ) ? "D is a": "D is not a");
Serial.println(" letter \r" );
}
void loop(){
}
Output
isascii() character function
If the ascii number of any character is used in the code this isascii() function tells If the number given is ascii of some character or not.
Serial.begin (9600);
Serial.print ("function isascii says:\r");
Serial.print (isascii( '$' ) ? "$ is a": "$ is not a");
Serial.println(" ascii \r" );
Serial.print ("function isascii says:\r");
Serial.print (isascii( 'E' ) ? "E is a": "E is not a");
Serial.println(" ascii \r" );
Serial.print ("function isalpha says:\r");
Serial.print (isascii( 'α' ) ? " α is a": "α is not a");
Serial.println(" ascii \r" );
Serial.print ("function isalpha says:\r");
Serial.print (isascii( 'ẟ' ) ? "ẟ is a": "ẟ is not a");
Serial.println(" ascii \r" );
}
void loop(){
}
Output
iscntrl() character function
To identify the controlling characters in the Arduino code like end of text(EOH) , end of transmission(EOT) ,acknowledge(ACK), backspace(BS), start of text (SOT), start of heading(SOH) and enquiry(ENQ) the iscontrol() character function is used. Similarly each of the control characters have their ascii. So here in example ascii are used:
Serial.begin (9600);
Serial.print ("function iscntrl says:\r");
Serial.print(iscntrl( '\n' ) ? "\\n is a ": "\\n is not a");
Serial.println(" control function \r" );
Serial.print ("function iscntrl says:\r");
Serial.print (iscntrl( '\t' ) ? "\\t is a": "\\t is not a");
Serial.println(" control function \r" );
Serial.print ("function iscntrl says:\r");
Serial.print (iscntrl( '\f' ) ? "\\f is a": "\\f is not a");
Serial.println(" control function \r" );
Serial.print ("function iscntrl says:\r");
Serial.print (iscntrl( '\s' ) ? "\\s is a": "\\s is not a");
Serial.println(" control function \r" );
}
void loop(){
}
Output
isprint() character function
In the Arduino program there are some characters which do not get displayed on the screen or in the output. So for the the identification of such characters isprint() character function is used:
Serial.begin (9600);
Serial.print ("function isprint says:\r");
Serial.print (isprint( 'Ø' ) ? " Ø is a": " Ø is not a");
Serial.println(" printing character \r" );
Serial.print ("function isprint says:\r");
Serial.print (isprint( '>' ) ? "> is a": "> is not a");
Serial.println(" printing character \r" );
}
void loop(){
}
Output
isalnum() character function
For the identification of the both letters and numbers the alphanumeric function that is isalnum() is used in Arduino programing:
Serial.begin (9600);
Serial.print ("function isalnum says:\r");
Serial.print (isalnum( '@' ) ? "@ is a": "@ is not a");
Serial.println(" is alpha numeric\r" );
Serial.print ("function isalnum says:\r");
Serial.print (isalnum( '1' ) ? "'1' is a": "'1' is not a");
Serial.println(" is alpha numeric\r" );
Serial.print ("function isalnum says:\r");
Serial.print (isalnum( 'a' ) ? "a is a": "a is not a");
Serial.println(" is alpha numeric\r" );
}
void loop(){
}
Output
isgraph() character function
This character function identifies characters which are printable as well as have some content in it. Similarly, if the character has a space but does not have any content then the isgraph() function will not consider it.
char character2 = 'd';
void setup() {
Serial.begin(9600);
Serial.println(isGraph(character1));
Serial.println(isGraph(character2));
}
void loop() {
}
Output
Conclusion
To store the values for characters in Arduino programming the character data type is used. Similarly, characters used to perform different types of functions. So, to identify the types of characters based on their properties different character functions are used. This write-up briefly explains the different types of character functions used in Arduino with the help of examples.