Spliter une chaine en fonction d'un caractère
Bonjour,
J'utilise la fonction str_split du tuto (chaines de caractères en c):
dont voici le code:
Code:
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
| char **str_split (char *s, const char *ct)
{
char **tab = NULL;
if (s != NULL && ct != NULL)
{
int i;
char *cs = NULL;
size_t size = 1;
/* (1) */
for (i = 0; (cs = strtok (s, ct)); i++)
{
if (size <= i + 1)
{
void *tmp = NULL;
/* (2) */
size <<= 1;
tmp = realloc (tab, sizeof (*tab) * size);
if (tmp != NULL)
{
tab = tmp;
}
else
{
fprintf (stderr, "Memoire insuffisante\n");
free (tab);
tab = NULL;
exit (EXIT_FAILURE);
}
}
/* (3) */
tab[i] = cs;
s = NULL;
}
tab[i] = NULL;
}
return tab;
} |
et voici mon code:
Code:
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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
char ** str_split (char *, const char *);
int main()
{
char *ch1= "bonjour monsieur le coiffeur" ;
int i;
char **t = NULL;
t=str_split (ch1," ");
i=0;
while (t[i]!=NULL)
{
printf("%d\n", t[i]);
i++;
}
return 0;
} |
Malheureusement ca plante et je ne vois pas pourquoi,
merci à vous !
User