All pastes #551030 Raw Edit

SPLIT em C/C++

public cpp v1 · immutable
·published 2007-06-08 16:49 UTC
rendered paste body
 #include <stdio.h> #include <stdlib.h> #include <string.h>char   **my_explode(char *str, char separator){  char **res = NULL;  int  nbstr = 1;  int  len;  int  from = 0;  int  i;  int  j;  res = (char **) malloc(sizeof (char *));  len = strlen(str);  for (i = 0; i <= len; ++i)    {      if ((i == len) || (str[i] == separator))       {         res = (char **) realloc(res, ++nbstr * sizeof (char *));         res[nbstr - 2] = (char *) malloc((i - from + 1) * sizeof (char));         for (j = 0; j < (i - from); ++j)             res[nbstr - 2][j] = str[j + from];         res[nbstr - 2][i - from] = '\0';         from = i + 1;         ++i;       }    }  res[nbstr - 1] =  NULL;  return res;}main(){ char* str= "PRIMEIRO|SEGUNDO|TERCEIRO"; char** tab;     tab = my_explode(str,"|");     printf("\n\n\n %s \n\n", tab[0]);}