Lesson 10.2. Null terminated string

Introduction

The particularity of strings compared to character arrays is that a string ends with a trailing zero. This zero zero at the end of the string corresponds to the first character of the ASCII table (not to be confused with the character '0' which has ASCII code 48). The character at the end of the string is not necessarily in the last cell of the array. Let's analyze the following string:

char text[8] = "Bye!";

Here is how this string is stored in memory:

Représentation en mémoire d'une chaîne de caractères

We can see that the zero at the end of the string is in the fifth cell which is not the last cell of the array. We can also see that the last 3 cells of the table contain arbitrary values, i.e. they are not are not initialized.

Syntax

The end of string character can be represented in different ways:

The zero at the end of the string solves the problem of passing parameters to functions. No need to add a second parameter for the size, it is the zero that indicates the end of the string.

Exercise

Exercise 1

We give the following string declaration:

char str[10] = "abc";

Using a loop, iterate through the characters of the string one by one and display them as :

Use sizeof() in the loop to get the array size. Here is the expected output:

str[0]  = 'a'   = 97    = 0x61
str[1]  = 'b'   = 98    = 0x62
str[2]  = 'c'   = 99    = 0x63
str[3]  = ''    =  0    = 0x00
str[4]  = ''    =  0    = 0x00
str[5]  = ''    =  0    = 0x00
str[6]  = ''    =  0    = 0x00
str[7]  = ''    =  0    = 0x00
str[8]  = ''    =  0    = 0x00
str[9]  = ''    =  0    = 0x00

Exercise 2

We provide the following string declaration:

char str[] = "This string is too long because it contains too many characters.";

In one instruction, truncate the string after the 23th character. Display the string before and after :

Before : This string is too long because it contains too many characters.
After  : This string is too long

Exercise 3

We provide the declaration of a string. We will assume that the string will never contain accents, cedilla, etc.

// Declaration of the string
char str[ ] = "This text should be capitalized.";

Complete the program below that converts each lowercase character in the string to uppercase. Note that it is converting the string and not just displaying it in upper case. Here is the expected result:

Before: This text should be capitalized.
After : THIS TEXT SHOULD BE CAPITALIZED.

Quiz

A string always ends with ....

Check Bravo! A string ends with a zero which is not necessarily in the last cell of the array. Try again...

What is the size of the following array:

char str[] = "abc";
Check Bravo! 3 characters in the string + the end of string zero = 4 bytes. Try again...

How many useful characters can the following string contain:

char str[8];
Check Bravo! If we remove the zero at the end of the string, there are 7 useful characters left. Try again...

Which syntaxes insert a trailing zero to mark the end of a string?

Check Bravo! Do not confuse the '0' character with the null-terminated character '\0'. Try again...

See also


Last update : 01/11/2023