In Arduino, character functions allow you to work with individual characters or strings of characters. These functions are particularly useful when handling text data, user input, or serial communication.
This tutorial explains the common character functions in Arduino and demonstrates their usage with examples.
Table of Contents
1. What Are Character Functions?
Character functions in Arduino allow you to:
- Check character properties (e.g., is it a letter, digit, or space?).
- Convert characters between upper and lower case.
- Work with individual characters in strings.
These functions are part of the <ctype.h> library, which Arduino includes by default.
2. Common Character Functions
2.1 isAlpha()
- Checks if a character is an alphabetic letter (a-z, A-Z).
- Returns true if it is a letter, otherwise false.
Example:
void setup() { Serial.begin(9600); char ch = 'A'; if (isAlpha(ch)) { Serial.println("This is an alphabetic character."); } else { Serial.println("This is not an alphabetic character."); } } void loop() { // Empty }
2.2 isDigit()
- Checks if a character is a digit (0-9).
- Returns true if it is a digit, otherwise false.
Example:
void setup() { Serial.begin(9600); char ch = '5'; if (isDigit(ch)) { Serial.println("This is a digit."); } else { Serial.println("This is not a digit."); } } void loop() { // Empty }
2.3 isAlnum()
- Checks if a character is alphanumeric (a-z, A-Z, 0-9).
- Returns true for letters or digits, otherwise false.
Example:
void setup() { Serial.begin(9600); char ch = '9'; if (isAlnum(ch)) { Serial.println("This is an alphanumeric character."); } else { Serial.println("This is not an alphanumeric character."); } } void loop() { // Empty }
2.4 isLowerCase()
- Checks if a character is a lowercase letter (a-z).
- Returns true if it is lowercase, otherwise false.
Example:
void setup() { Serial.begin(9600); char ch = 'm'; if (isLowerCase(ch)) { Serial.println("This is a lowercase letter."); } else { Serial.println("This is not a lowercase letter."); } } void loop() { // Empty }
2.5 isUpperCase()
- Checks if a character is an uppercase letter (A-Z).
- Returns true if it is uppercase, otherwise false.
Example:
void setup() { Serial.begin(9600); char ch = 'M'; if (isUpperCase(ch)) { Serial.println("This is an uppercase letter."); } else { Serial.println("This is not an uppercase letter."); } } void loop() { // Empty }
2.6 toLowerCase()
- Converts a character to lowercase if it is uppercase.
- Returns the converted character.
Example:
void setup() { Serial.begin(9600); char ch = 'M'; char lower = toLowerCase(ch); Serial.print("Lowercase: "); Serial.println(lower); } void loop() { // Empty }
2.7 toUpperCase()
- Converts a character to uppercase if it is lowercase.
- Returns the converted character.
Example:
void setup() { Serial.begin(9600); char ch = 'm'; char upper = toUpperCase(ch); Serial.print("Uppercase: "); Serial.println(upper); } void loop() { // Empty }
2.8 isSpace()
- Checks if a character is a whitespace character (e.g., space, tab).
- Returns true if it is whitespace, otherwise false.
Example:
void setup() { Serial.begin(9600); char ch = ' '; if (isSpace(ch)) { Serial.println("This is a whitespace character."); } else { Serial.println("This is not a whitespace character."); } } void loop() { // Empty }
3. String to Character Conversion
Characters within strings can be accessed and manipulated using their index.
Example: Access Characters in a String
void setup() { Serial.begin(9600); String text = "Arduino"; for (int i = 0; i < text.length(); i++) { Serial.print("Character at index "); Serial.print(i); Serial.print(": "); Serial.println(text[i]); } } void loop() { // Empty }
4. Practical Examples
4.1 Check for Alphanumeric Characters
void setup() { Serial.begin(9600); char input[] = "Arduino123"; for (int i = 0; i < strlen(input); i++) { if (isAlnum(input[i])) { Serial.print(input[i]); Serial.println(" is alphanumeric."); } else { Serial.print(input[i]); Serial.println(" is not alphanumeric."); } } } void loop() { // Empty }
4.2 Convert Text Case
void setup() { Serial.begin(9600); char text[] = "Arduino is Fun!"; for (int i = 0; i < strlen(text); i++) { if (isLowerCase(text[i])) { text[i] = toUpperCase(text[i]); } else if (isUpperCase(text[i])) { text[i] = toLowerCase(text[i]); } } Serial.println(text); } void loop() { // Empty }
4.3 Validate User Input
void setup() { Serial.begin(9600); } void loop() { if (Serial.available() > 0) { char input = Serial.read(); if (isAlpha(input)) { Serial.println("You entered a letter."); } else if (isDigit(input)) { Serial.println("You entered a digit."); } else if (isSpace(input)) { Serial.println("You entered a space."); } else { Serial.println("Unknown character."); } } }
5. Best Practices for Using Character Functions
- Validate Input:
- Use isAlpha(), isDigit(), and similar functions to validate user input.
- Use Loops for Strings:
- Iterate through strings to process characters efficiently.
- Be Aware of Case:
- Normalize text by converting all characters to lowercase or uppercase if necessary.
- Optimize for Memory:
- Use character arrays (char[]) instead of String objects in memory-critical applications.
Conclusion
Character functions in Arduino make it easy to work with individual characters and text data.
Whether you’re validating input, converting case, or analyzing characters, these functions provide a flexible and powerful way to handle text.
This tutorial covered the most commonly used functions and practical examples to help you get started.
For more details, visit the official Arduino reference.