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

Arduino string


May 15, 2021 Arduino


Table of contents


Strings are used to store text. /b10> They can be used to display text in lcd or Arduino IDE serial monitor windows. /b11> Strings can also be used to store user input. /b12> For example, a character typed by a user on a keyboard connected to Arduino.

There are two types of strings in Arduino programming:

  • An array of characters, the same string used in C programming.
  • Arduino string, which allows us to use string objects in sketches.

In this chapter, we'll learn about the use of strings, objects, and strings in the Arduino sketch. /b10> At the end of this chapter, you'll learn what type of string to use in your sketch.

An array of string characters

The first type of string we want to learn is a series of characters of the char type. /b10> In the previous chapter, we learned what an array is: a continuous sequence of variables of the same type stored in memory. A string is an array of char variables.

A string is a special array with an extra element at the end of the string whose value is always 0 (zero). /b10> This is called an "empty termination string."

Example of an array of string characters

This example shows how to create a string and print it to the serial monitor window.

Cases

void setup() {
   char my_str[6]; // an array big enough for a 5 character string
   Serial.begin(9600);
   my_str[0] = 'H'; // the string consists of 5 characters
   my_str[1] = 'e';
   my_str[2] = 'l';
   my_str[3] = 'l';
   my_str[4] = 'o';
   my_str[5] = 0; // 6th array element is a null terminator
   Serial.println(my_str);
}

void loop() { 

}

The following example shows what a string consists of. /b10> An array of characters with printable characters and 0 as the last element of the array, indicating that this is where the string ends. /b11> By using Serial.println() and passing the name of the string, you can print the string to the Arduino IDE serial monitor window.

The same example can be written in a more convenient way, as follows:

Example

void setup() {
   char my_str[] = "Hello";
   Serial.begin(9600);
   Serial.println(my_str);
}

void loop() {

}

In this sketch, the compiler calculates the size of the string array and automatically terminates the string with an empty value of 0. /b10>An array of six elements long, with five characters followed by a zero, is created exactly the same way as the previous sketch.

An array of action strings

We can change the string array in the sketch, as shown in the following image.

Example

void setup() {
   char like[] = "I like coffee and cake"; // create a string
   Serial.begin(9600);
   // (1) print the string
   Serial.println(like);
   // (2) delete part of the string
   like[13] = 0;
   Serial.println(like);
   // (3) substitute a word into the string
   like[13] = ' '; // replace the null terminator with a space
   like[18] = 't'; // insert the new word
   like[19] = 'e';
   like[20] = 'a';
   like[21] = 0; // terminate the string
   Serial.println(like);
}

void loop() {

}

Results

I like coffee and cake
I like coffee
I like coffee and tea

The sketch above works as follows.

(1) Create and print strings

In the sketch given above, a new string is created and printed out to display in the serial monitor window.

(2) Shorten the string

Shorten the string by replacing the 14th character in the string with an empty termination of 0. /b10> This is the 13th element in an array of strings calculated from 0.

When you print a string, all characters are printed to the new empty termination 0. /b10> Other characters do not disappear; they still exist in memory, and the string array is still the same size. /b12> The only difference is that any function that uses strings can only see strings before the first empty terminator.

(3) Change the word in the string

Finally, the sketch replaces the word "cake" with "tea". /b10>It must first replace the space terminator with a space, such as . . . in order to restore the string to its original format.

The new character overrides the word "cake" with the word "tea". /b10> This is done by overwriting a single character. /b11> The "e" of "cake" is replaced with the new empty termination character. /b12> The result is that the string actually terminates with two empty characters, the original character at the end of the string, and a new character that replaces the "e" in "cake". /b13> This makes no difference when printing a new string, because the function of the print string stops printing string characters when the first empty termination character is encountered.

A function that manipulates an array of strings

The last sketch manipulates the string manually by accessing a single character in the string. /b10> To make it easier to manipulate an array of strings, you can write your own functions to execute them, or you can use some string functions in the C language library.

The list function for the array of action strings is shown below.

The next sketch uses some C-string functions.

Example

void setup() {
   char str[] = "This is my string"; // create a string
   char out_str[40]; // output from string functions placed here
   int num; // general purpose integer
   Serial.begin(9600);

   // (1) print the string
   Serial.println(str);

   // (2) get the length of the string (excludes null terminator)
   num = strlen(str);
   Serial.print("String length is: ");
   Serial.println(num);

   // (3) get the length of the array (includes null terminator)
   num = sizeof(str); // sizeof() is not a C string function
   Serial.print("Size of the array: ");
   Serial.println(num);

   // (4) copy a string
   strcpy(out_str, str);
   Serial.println(out_str);

   // (5) add a string to the end of a string (append)
   strcat(out_str, " sketch.");
   Serial.println(out_str);
   num = strlen(out_str);
   Serial.print("String length is: ");
   Serial.println(num);
   num = sizeof(out_str);
   Serial.print("Size of the array out_str[]: ");
   Serial.println(num);
}

void loop() {

}

Results

This is my string
String length is: 17
Size of the array: 18
This is my string
This is my string sketch.
String length is: 25
Size of the array out_str[]: 40

The sketch above works as follows.

(1) Print the string

The newly created string is printed to the serial monitor window, as completed by the previous sketch.

(2) Get the length of the string

The strlen() function is used to get the length of the string. /b10> The length of the string is for printable characters only and does not include an empty terminator.

The string contains 17 characters, so we see 17 characters in the serial monitor window.

(3) Get the length of the array

The operator sizeof() is used to get the length of the array containing the string. /b10> The length includes an empty terminator, so the length is 1 more than the length of the string.

sizeof() looks like a function, but technically an operator. /b10> It is not part of the C string library, but is used in sketches to show the difference between array size and string size (or string length).

(4) Copy the string

The strcpy() function is used to copy the str?string to out_num of the str?string. /b10> The strcpy() function copies the second string passed to it into the first string. N ow, a copy of the string exists in the out_num array, but only 18 elements of the array are used, so there are still 22 idle char elements in the array. /b12> These free elements can be found behind strings in memory.

Copy the string into the array so that we have some extra space in the array for the next part of the sketch, which is to add a string at the end of the string.

(5) Attach string to string (connection)

A sketch adds one string to another, which is called in series. /b10> This is done using the strcat() function. /b11> The strcat() function places the second string passed to it at the end of the first string passed to it.

After series, the length of the string is printed to show the new string length. /b10> The length of the array is then printed to show a string of 25 characters in an array of 40 elements long.

Keep in mind that a string of 25 characters in length actually takes up 26 characters of the array because there is an empty termination of 0.

Array boundaries

When working with strings and arrays, it is important to work within the boundaries of strings or arrays. /b10> In the sample sketch, an array of 40 characters is created to allocate memory that can be used to manipulate strings.

If the array is too small and we try to copy a string larger than the array, the string is copied beyond the end of the array. /b10> Memory beyond the end of the array may contain other important data used in the sketch, but they will be overwritten by strings. /b11> If memory beyond the end of the string is out of range, it may cause the sketch to crash or cause unexpected behavior.