What is a string? How to do operations? How to convert a string's number into an integer ?

Introduction

String variables allow you to store strings in your programs. In a string you can store any kind of Ascii character. Thanks to this type of variable you can write messages for example.

Here is how to declare a string:

What is the ASCII table?

The ASCII table is the set of characters you can use in your string. Here is a table that summarizes all these characters:

In this table we will only use the char column. Indeed, in this column, you will find all the characters that you can put in your variable.

What is the difference between a char and a string?

The char and string variables are quite similar: they both allow to store strings. However, char variables are more used to store a character or to access a character in a string. Whereas a string variable is more used to store several characters and to do operations on them, like concatenate them for example.

We will see at the end of this course how to convert a string to a char.

Operation on string's variable

We will now see different operations that we can do on strings:

A) Find the place of an element in a string

Here is how to know the place of an element in a string:

We can give you an example to better understand its use:

If the value is not found in your string, the program returns -1.

B) The size of your string

We will learn know how to find the size of your string:

Here is a detailed example :

C) Compare two strings

We will now see how to compare two strings together:

Here are the possible returns of this function:

  • The number 0 is returned if both strings are identical.
  • A negative number is given if my_string_1 is placed in front of my_string_2.
  • A positive number is given if my_string_2 is placed in front of my_string_1.

We can give you an example to better understand its use:

D) Replacing one of the elements of the string

We will now see how to replace an element of a string by an another one:

We can give you an example to better understand its use:

This is what we have: “the_new_stringrduino Factory”.

E) Put your string in uppercase

We will now see how to put your string in uppercase :

We can give you an example to better understand its use:

F) How to put your string in lower case

We will now see how to put your string in lower case:

We can give you an example to better understand its use:

Convert a string to float, int or char

We will now convert a string into a variable of another type, int, float or char.

A) Converting a string into an integer

We will now see how to convert a string into an integer.

Your string must be exclusively integers numbers for this to work. Otherwise you will get an error.

We can give you an example to better understand its use:

B) Converting a string into a float


We will now see how to convert a string into a decimal number (float):

Your string must be exclusively decimal numbers for this to work. Otherwise you will get an error.

We can give you an example to better understand its use:

C) Converting a string to a char


We will now see how to convert a string into a char.

We can give you an example to better understand its use: