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 = "Bye!";
Here is how this string is stored in memory:
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.
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.
We give the following string declaration:
char str = "abc";
Using a loop, iterate through the characters of the string one by one and display them as :
sizeof() in the loop to get the array size.
Here is the expected output:
str = 'a' = 97 = 0x61 str = 'b' = 98 = 0x62 str = 'c' = 99 = 0x63 str = '' = 0 = 0x00 str = '' = 0 = 0x00 str = '' = 0 = 0x00 str = '' = 0 = 0x00 str = '' = 0 = 0x00 str = '' = 0 = 0x00 str = '' = 0 = 0x00
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
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.
A string always ends with ....
What is the size of the following array:
char str = "abc";
How many useful characters can the following string contain:
Which syntaxes insert a trailing zero to mark the end of a string?
'0'character with the null-terminated character
'\0'. Try again...