Comment diviser une chaîne en jetons, puis les sauvegarder dans un tableau?
Plus précisément, j’ai une chaîne "abc/qwe/jkh"
. Je souhaite séparer "/"
, puis enregistrer les jetons dans un tableau.
La sortie sera telle que
array[0] = "abc" array[1] = "qwe" array[2] = "jkh"
Aidez-moi, s’il vous plaît
#include #include int main () { char buf[] ="abc/qwe/ccd"; int i = 0; char *p = strtok (buf, "/"); char *array[3]; while (p != NULL) { array[i++] = p; p = strtok (NULL, "/"); } for (i = 0; i < 3; ++i) printf("%s\n", array[i]); return 0; }
Vous pouvez utiliser strtok()
char ssortingng[]= "abc/qwe/jkh"; char *array[10]; int i=0; array[i] = strtok(ssortingng,"/"); while(array[i]!=NULL) { array[++i] = strtok(NULL,"/"); }