Learn to code

Arduino Home

Lessons 1
Arduino - Getting Started
Lessons 2
Arduino - Basics
Lessons 3
Arduino - Serial Communication
Lessons 4
Arduino - Digital / Analog
Lessons 5
Arduino - Visual Output
Lessons 6
Arduino - Motor Control
Lessons 7
Arduino -LCD Displays
Lessons 8
Arduino -LCD Displays

Understanding character strings in arduino

in Arduino Basics

As stated in the lesson about String (with capital S), you can use the String class, a special data type for Arduino ID, or make a string out of an array of characters. The characters in the array are formatted according to the ASCII characters.  Another important element is that every string needs to have a final null character to tell other functions where the string ends. Every time we declare a string, we need to leave room for the terminating null character.

First, we dive into what a character is. Then we will discuss the array of strings (character string). In the end, we will finally our ready for some coding.

This lesson will focus on the string of characters. For more information about the String object, which is a bit easier (at the cost of more memory usage), see the String object tutorial.

What is a character?

The first element is of the character string that we need to learn is the character data type. Characters have a capacity of one-byte memory allocation. It can store values between 0 and 255. Characters are stored as numbers but are encoded as ASCII text. The ASCCI chart allows you to present text, symbols, or characters as numbers.

Each letter has a number associated with it. Each numeric value also has a number associated with it. With ASCII encoding, you can write down some numbers and pass it to a Serial Monitor or another Arduino board. With the ASCII chart, you can encode these numbers.

To declare a character, you just have to use the word char to declare the character and attach a variable name that holds the character. Next, you initialize the character by using an equal sign and the character that you would like.

char Letter = 'A';

Another way of representing the letter A is to use the number associated with it in the ASCII chart. So we could rewrite char Letter with the same end result. The variable name Letter is now holding the number 65, which is encoded into ASCII.

char Letter = 65;

So, there are two possibilities when using a single character. You could write initialize with the number from the ASCII chart or use the letter or number you want, with single quotes, and the encoding is done for you by the compiler.

It seems a bit awkward to use a number to encode a number. But in fact, what you do is using a symbol that represents a number. Below is the table of the ASCII characters. This is not the full ASCII table.

Table 1, ASCII characters

what is a character string 

Followed from characters comes an array of characters called string or c-string. A string, with lowercase s, is a couple of characters in a row. The characters in the row are enclosed with double quotes “  “. The string looks a bit like declaring and initializing an array. A simple character array might look as the following:

char Greeting[] = "Hello everyone";

The example above is a c-style text string with a char data type that contains the text “Hello everyone.”  The length of the array is equal to all the different characters in the array.

If you count the letters and spacing, you will see that there are only 14 characters in the string. Because strings need a null terminating character or 0 byte that tells other functions that the string ends. Without the null character functions like Serial.println()  would continue to access parts of the memory used for other things. When you declare a string without a number of elements, the compiler will determine the array’s proper dimension.

So, It seems that the string has a collection of 14 elements, each stored as a separate element in the array. Our string named Greeting will be sized with 15 elements and, therefore, will contain 20 bytes.

You could also rewrite the string above to determine the dimension size when you declared the string.

char Greeting[14] = "Hello everyone";

Ensure that your string’s size (when you declare it yourself), dimension, is one larger than the number of elements in the string. There are many more ways to declare strings, and we will look at them below.

Terminating null character: This allows functions like Serial.print() to tell where the end of the string is. You can set the dimension of the string yourself or let the compiler do it for you.

Creating a Character string

Creating a Character string: is very easy to do. Below are some ways you could create a c-string. The code below shows some examples of how you can declare strings.

char One[12];
char Two[6] = {'H', 'e', 'l', 'l', 'o'};
char Three[6] = {'H', 'e', 'l', 'l', 'o', ''};
char Four[] = "Hello";
char Five[6] = "Hello";
char Six[12] = "Hello";

Declaring a string

  • Declare an array of chars without initializing it as in string One
  • Declare an array of chars (with one extra char), and the compiler will add the required null character, as in string Two.
  • Explicitly add the null character, string Three
  • Initialize with a string constant in quotation marks; the compiler will size the array to fit the string constant and a terminating null character, string 4
  • Initialize the array with an explicit size and string constant, string 5
  • Initialize the array, leaving extra space for a larger string, string 6
char One[12];
char Two[6] = {'H', 'e', 'l', 'l', 'o'};
char Three[6] = {'H', 'e', 'l', 'l', 'o', ''};
char Four[] = "Hello";
char Five[6] = "Hello";
char Six[12] = "Hello";

To clarify the above a bit more, let us initialize a string in the sketch. We can do this with characters or with ASCII codes.

/*creating a character string
 * with 5 elements and a six element
 * for the terminating null character
*/
char Name[6];
void setup() {

}
Name[0] = "H";
Name[1] = "e";
Name[2]= "l";
Name[3]= "l";
Name[4]= "o";

void loop() {

Serial.print(Name);

}

Declaration with ASCII codes, it is:

Name[0] = 72; // capital H in ASCII
Name[1] = 101; // lowercase e in ASCII
Name[2]= "108"; // lowercase l in ASCII
Name[3]= "108"; // lowercase l in ASCII
Name[4]= "111"; // lowercase o in ASCII

As you can see, you could do this with ASCII codes or with characters. Remember to use the correct quotes.

It may be easier to understand strings by giving a graphic overview of the string we made above:

Figure 2, graphical view of the string “hello”.

Example code function of strings

It is time to look at a couple of functions that can be used. We will discuss a couple of these functions and give an overview of functions you can try for yourself.

First, let’s create a sketch with some character strings.

char name[9];
char name2[9] = "platform";
char numbers[] = "1234";
char numbers2[] = "4321";  

Change the content of string: How can you place something in the char variable name in your sketch?  The below code snippet will not work.

name = "Arduino"; // This will not work

Since you are using a character array, you will need to change each of the characters individually. So to change the content of the string, you can use the strcpy() function.

strcpy(stringname, "new characters");

The above function will iterate over each character in the new string and place them into the old string.

So to fill the character string name with the word “Arduino,” we can add the following function in the sketch.

strcpy(name, “Arduino”);

Add string together

Another useful function is to add strings together. For this, we need to declare another character variable that holds the new character string.

Another function is to add character together with the following function:

strcat(string destination, string name to add)

For example:

char name[10] = "platform";
char name2[9] = "Arduino";
char all[20] = "";

strcat(all, name); 
Serial.print(all);
strcat(all, name2);
Serial.Print(all);

Compare character string

A difficult part of working with character strings is to know what is in the string. If you want to compare strings, you cannot compare them as if they were integer variables. If you want to compare strings, you have to deal with individual characters.

char number[5] = "1234"
char number2[5] = "4321";
char number3[5];

if (number == number2) {
  
Do something

}

Unfortunately, the above will not work. If you want to compare strings you have to compare the content of the memory character by character.

There is a function that will compare character strings, it is called sctrcmp.  Strcmp will take two strings and compare them character by character. If the strings are the same the result is 0. If they are not the same the result is -1 or +1. That depends on what the outcome of your comparison is in the ASCII table.

To compare the string from above:

// this wil be FALSE in our case 
if strcmp(number, number2) == 0)  
{
Do something
} 

You could also use this function with a string inside the strcmp function. However, you will need to use the double quote for this. Use a single quote if you were comparing one character.

if strcmp(number, "1234") == 0)  // this wil be True
{
Do something
} 

There are many more string functions that you can use. Below is a short overview of some most used string functions.

string function table

FunctionDescription
strlen()calculate string's length
strcpy()Copies a string to another string
strcat()Joins two strings
strcmp()Compares two strings
strcmp()Converts string to lowercase
strupr()Converts string to uppercase

Hopefully, you can distinguish Strings from strings after learning the two tutorials, and you will be able to use them in your sketch.

Previous Post
Using Switch Statements in Arduino
You must be logged in to post a comment.
Menu