Académique Documents
Professionnel Documents
Culture Documents
B o n j o u r \0
t
ss
B o n s o i r \0
Chanes de caractres
Affichage
char
chart[]t[]=="Bon\0jour\0";
"Bon\0jour\0";
printf("%s",t);
printf("%s",t); /*/*affiche
afficheBon
Bon*/*/
Chanes de caractres
Lecture
char
charstr[10];
str[10];
printf("Enterez
printf("Enterezvotre
votrenom
nom: :");
");
scanf("%s",
scanf("%s",str);
str); /*/*pas
pasde de&&car
carstr
strest
estune
unerfrence*/
rfrence*/
scanf("%9s",
scanf("%9s",str);
str); /*/*on
onlimite
limite99pour
pour\0\0*/*/
Fonctions de manipulation de
chanes
Le fichier en-tte string.h contient des dclarations de
fonctions permettant la manipulation de chanes de caractres
strlen(s) : retourne la longueur de la chane s sans compter
le '\0' final
char
char*str=Bonjo\0ur;
*str=Bonjo\0ur;
int
intLL=strlen(str);
=strlen(str);
printf("%d",L);
printf("%d",L); /*/*affiche
affiche55*/*/
#include<stdio.h> #include<stdio.h>
#include<string.h> #include<string.h>
int main (){ int main (){
char t[] = "Bon\0jour\0"; char t[] = "Bon\0jour\0";
char *s; char s[10];
for(int i=0; i<strlen(t); i++) for(int i=0; i<strlen(t); i++)
printf(" %c ",t[i]); printf(" %c ",t[i]);
strcpy(s,t);
strcpy(s,t); printf("%s", s); /* affiche Bon*/
printf("%s", s); strcat(s,t);
/* ERREUR : allocation de mmoire!! */ printf("%s", s); /* affiche BonBon*/
printf("%s", t); /* Affiche Bon*/
printf("%s",strchr(s,n'));