// Une implémentation de la fonction split #include <stdio.h> #include <stdlib.h> #include <string.h> char **split(const char *str, const char delim, int *nb) { char **tab = NULL; char mot[200]; // anticonstitutionnellement en fait 26 int b=0, n=0, len=0, item=0; tab = calloc(*nb, sizeof(char *)); if (!tab) {perror("calloc()"); return tab;} len = strlen(str); for (n=0; n<=len; n++) { if (str[n]==delim || n==len) { // réalloue espace du tableau tab = (char **) realloc(tab, sizeof(char *) * (item+1)); if (!tab) {perror("realloc()"); return tab;} // alloue espace pour nouveau mot tab[item] = malloc(b+1); if (!tab[item]) {perror("malloc()"); return tab;} //insère mot au tableau strncpy(tab[item], mot, b); tab[item][b]=0; // le termine par '\0' *nb = ++item; b=0; } else mot[b++]=str[n]; } return tab; } int main () { char **t; int nb=0, n; t = split("Ceci est une phrase à découper.", ' ', &nb); printf("La phrase fait %d mots.\n", nb); for (n=0; n<nb; n++) printf("\t%d: [%s]\n", n, t[n]); free(t); return 0; }