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 34 35 36 37 38 39 40 41 42 43
| #include <stdlib.h>
#include <stdio.h>
int main(void)
{
static char * strEntree[4] = {
"titi=toto",
"fifi = riri",
" beber= jaja",
" jeanjean = loulou",
};
char * gauche[4] = { NULL };
char * droite[4] = { NULL };
int i = 0;
char * posEgal = NULL;
for (;i<4; ++i) {
size_t lgEntree = strlen(strEntree[i]);
posEgal = strchr(strEntree[i], '=');
gauche[i] = malloc((posEgal-strEntree[i]+1)*sizeof(*gauche[i]));
strncpy(gauche[i], strEntree[i], posEgal-strEntree[i]);
gauche[i][posEgal-strEntree[i]] = '\0';
droite[i] = malloc((strEntree[i]+lgEntree-posEgal+1)*sizeof(*gauche[i]));
strncpy(droite[i], posEgal+1, strEntree[i]+lgEntree-posEgal);
droite[i][strEntree[i]+lgEntree-posEgal] = '\0';
}
for (i=0;i<4; ++i) {
printf("G:%s D:%s\n", gauche[i], droite[i]);
}
for (i=0;i<4; ++i) {
free(gauche[i]); free(droite[i]);
}
return 0;
} |
Partager