1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #include <stdlib.h>
#include <stdio.h>
int longueur_chaineP(char *str);
int longueur_chaineT(char *str);
int main ()
{
char *str = "developpez.org"; // concrétement en mémoire : developpez.org\0
printf("%c", str[0]); // affiche d
printf("%c", *str); // affiche d
printf("%c", *++str); // affiche e
printf("%d", longueur_chaineT(str)); // longueur chaine avec tableaux
printf("%d", longueur_chaineP(str)); // meme chose avec pointeurs
return EXIT_SUCCESS;
}
int longueur_chaineT(char *str) // en utilisant les tableaux
{
int nb;
for(nb = 0; str[nb]; nb++); // str[nb] != '\0' si tu préfères
return nb;
}
int longueur_chaineP(char *str)
{
int nb;
for(nb = 0; *str; *str++, nb++);
return nb ;
} |
Partager