Strings: char arrays and the null terminator · Chaînes : tableaux de caractères et terminateur nul
Text is an array of chars
- C has no separate string type. A string is just an array of
char. char word[] = "hi";actually stores'h','i', and a hidden'\0'at the end.- You can read one character at a time with an index, exactly like any array.
Le texte est un tableau de chars
- C n'a pas de type chaîne séparé. Une chaîne est simplement un tableau de
char. char word[] = "hi";stocke en réalité'h','i', et un'\0'caché à la fin.- Vous pouvez lire un caractère à la fois avec un index, exactement comme n'importe quel tableau.
The invisible terminator '\0'
- Every C string ends with a special null terminator, written
'\0'(a byte equal to0). - It marks where the text stops. The word
"hi"takes three bytes:'h','i','\0'. - Functions know a string has ended when they reach
'\0'— there is no stored length.
Le terminador invisible '\0'
- Toute chaîne C se termine par un terminateur nul spécial, noté
'\0'(un octet égal à0). - Il marque où s'arrête le texte. Le mot
"hi"prend trois octets :'h','i','\0'. - Les fonctions savent qu'une chaîne de caractères est terminée lorsqu'elles atteignent
'\0'— il n'y a pas de longueur stockée.
Walking a string
- To visit each character, loop an index while
s[i] != '\0'. - When
s[i]becomes'\0', you have reached the end — stop. - This is how you count characters, search, or copy a string by hand.
Parcourir une chaîne
- Pour visiter chaque caractère, bouclez sur un index tant que
s[i] != '\0'. - Quand
s[i]devient'\0', vous avez atteint la fin — arrêtez. - C'est ainsi que vous comptez des caractères, cherchez ou copiez une chaîne manuellement.
#include <stdio.h>
int main(void) {
char word[] = "hello";
int len = 0;
while (word[len] != '\0') { // stop at the terminator
len++;
}
printf("%d\n", len); // 5
return 0;
}
The <string.h> toolbox
<string.h>has ready-made helpers:strlen(s)(length),strcpy(dst, src)(copy),strcmp(a, b)(compare),strcat(dst, src)(join).strcmp(a, b)returns0when the two strings are equal — handy in tests.- These all rely on the
'\0'terminator to know where each string ends.
La boîte à outils <string.h>
<string.h>a des helpers prêts à l'emploi :strlen(s)(longueur),strcpy(dst, src)(copie),strcmp(a, b)(comparaison),strcat(dst, src)(join).strcmp(a, b)retourne0quand les deux chaînes sont égales — utile dans les tests.- Toutes ces fonctions dépendent du terminateur
'\0'pour savoir où s'arrête chaque chaîne.
Common mistakes
- A string needs one extra byte for
\0: a 5-letter word needschar[6]. - Compare strings with
strcmp, not==.
Erreurs courantes
- Une chaîne a besoin d'un octet supplémentaire pour le
\0: un mot de 5 lettres nécessitechar[6]. - Comparez les chaînes avec
strcmp, pas==.
Now you try
- To change a string in place, the caller must pass a writable
chararray, not a fixed text. - Stop your loops at
'\0'. Do not write amain— the checker provides one.
À vous maintenant
- Pour modifier une chaîne in-place, l'appelant doit passer un tableau
charmodifiable, pas du texte fixe. - Arrêtez vos boucles au
'\0'. Ne writez pas demain— le vérificateur en fournit un.
Strings are arrays of characters · Les chaînes sont des tableaux de caractères
Index each character from 0; the slice shows a range of chars. · Indexez chaque caractère à partir de 0 ; la tranche montre une plage de caractères.
Complete int count_char(const char *s, char c) so it returns how many times c appears in the string s. Loop until you reach the end of the string. Do not · non write a main. · Complétez int count_char(const char *s, char c) pour qu'il retourne combien de fois c apparaît dans la chaîne s. Bouclez jusqu'à atteindre la fin de la chaîne. Ne pas écrire de main.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Complete int my_strlen(const char *s) so it returns the length of s without calling strlen. Walk the array until you reach the end. Do not · non write a main. · Complétez int my_strlen(const char *s) pour qu'il retourne la longueur de s sans appeler strlen. Parcourez le tableau jusqu'à atteindre la fin. Ne pas écrire de main.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Complete void reverse_str(char *s) so it reverses the characters of s in place. Find the length, then swap from both ends toward the middle. Do not · non write a main. · Complétez void reverse_str(char *s) pour qu'il inverse les caractères de s in-place. Trouvez la longueur, puis échangez des deux extrémités vers le centre. Ne pas écrire de main.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.