Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

delimter in c

/*
a delimter function, that returns an array of strings dynamically allocated
and splitted according to one char 
*/
char** split(const char* str, char delimiter){

    char** arr = NULL;
    const char* p;

    if(str == NULL)
        return NULL;

    unsigned int arrsize = 0;

    for(p=str; *p; p++) {

        if(*p == delimiter) {

            arr = (char**)realloc(arr, sizeof(char*)*(++arrsize));
            arr[arrsize-1] = (char*)calloc(sizeof(char), (p-str)+1);
            strncpy(arr[arrsize-1], str, p-str);
            str = p+1;

        }

    }

    // Last item
    arr = (char**)realloc(arr, sizeof(char*)*(++arrsize));
    arr[arrsize-1] = (char*)calloc(sizeof(char), (p-str)+1);
    strncpy(arr[arrsize-1], str, p-str);
    return arr;
}
 
PREVIOUS NEXT
Tagged: #delimter
ADD COMMENT
Topic
Name
1+7 =