Character Analysis

Use the operators to recognise the type of character we are dealing with.

In this example we use the operators that allow us to recognise the type of character we are dealing with. It is useful to check if a character is ASCII, or is upper case, or numeric, or it is a punctuation mark and so forth. The options available cover a variety of situations and this is demonstrated in the sketch below. Every character sent to the board through the serial monitor of the Arduino Software (IDE) is analysed by the sketch that returns all the information it was able to find. A single character may trigger more than one condition and therefore you may get multiple answers for a single entry.

The available operators are:

  • isAlphaNumeric() it's alphanumeric

  • isAlpha() it's alphabetic

  • isAscii() it's ASCII

  • isWhitespace() it's whitespace

  • isControl() it's a control character

  • isDigit() it's a numeric digit

  • isGraph() it's a printable character that's not whitespace

  • isLowerCase() it's lower case

  • isPrintable() it's printable

  • isPunct() it's punctuation

  • isSpace() it's a space character

  • isUpperCase() it's upper case

  • isHexadecimalDigit() it's a valid hexadecimaldigit (i.e. 0 - 9, a - F, or A - F)

Hardware Required

  • Arduino Board

Circuit

There is no circuit for this example, though your board must be connected to your computer via USB and the serial monitor window of the Arduino Software (IDE) should be open.

circuit

Code

Open the serial monitor window of the Arduino Software (IDE) and type in a single character at a time, then press Send to get a report about that specific character.

1/*
2
3 Character analysis operators
4
5 Examples using the character analysis operators.
6
7 Send any byte and the sketch will tell you about it.
8
9 created 29 Nov 2010
10
11 modified 2 Apr 2012
12
13 by Tom Igoe
14
15 This example code is in the public domain.
16
17 https://www.arduino.cc/en/Tutorial/CharacterAnalysis
18
19*/
20
21void setup() {
22
23 // Open serial communications and wait for port to open:
24
25 Serial.begin(9600);
26
27 while (!Serial) {
28
29 ; // wait for serial port to connect. Needed for native USB port only
30
31 }
32
33 // send an intro:
34
35 Serial.println("send any byte and I'll tell you everything I can about it");
36
37 Serial.println();
38}
39
40void loop() {
41
42 // get any incoming bytes:
43
44 if (Serial.available() > 0) {
45
46 int thisChar = Serial.read();
47
48 // say what was sent:
49
50 Serial.print("You sent me: \'");
51
52 Serial.write(thisChar);
53
54 Serial.print("\' ASCII Value: ");
55
56 Serial.println(thisChar);
57
58 // analyze what was sent:
59
60 if (isAlphaNumeric(thisChar)) {
61
62 Serial.println("it's alphanumeric");
63
64 }
65
66 if (isAlpha(thisChar)) {
67
68 Serial.println("it's alphabetic");
69
70 }
71
72 if (isAscii(thisChar)) {
73
74 Serial.println("it's ASCII");
75
76 }
77
78 if (isWhitespace(thisChar)) {
79
80 Serial.println("it's whitespace");
81
82 }
83
84 if (isControl(thisChar)) {
85
86 Serial.println("it's a control character");
87
88 }
89
90 if (isDigit(thisChar)) {
91
92 Serial.println("it's a numeric digit");
93
94 }
95
96 if (isGraph(thisChar)) {
97
98 Serial.println("it's a printable character that's not whitespace");
99
100 }
101
102 if (isLowerCase(thisChar)) {
103
104 Serial.println("it's lower case");
105
106 }
107
108 if (isPrintable(thisChar)) {
109
110 Serial.println("it's printable");
111
112 }
113
114 if (isPunct(thisChar)) {
115
116 Serial.println("it's punctuation");
117
118 }
119
120 if (isSpace(thisChar)) {
121
122 Serial.println("it's a space character");
123
124 }
125
126 if (isUpperCase(thisChar)) {
127
128 Serial.println("it's upper case");
129
130 }
131
132 if (isHexadecimalDigit(thisChar)) {
133
134 Serial.println("it's a valid hexadecimaldigit (i.e. 0 - 9, a - F, or A - F)");
135
136 }
137
138 // add some space and ask for another byte:
139
140 Serial.println();
141
142 Serial.println("Give me another byte:");
143
144 Serial.println();
145
146 }
147}

Learn more

You can find more basic tutorials in the built-in examples section.

You can also explore the language reference, a detailed collection of the Arduino programming language.

Last revision 2015/08/11 by SM

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.