Split text to words with memory allocation

67 Views Asked by At

I have a task where I need to create function to split given text into words and save them in array of string. But function requires create and return a double pointer (pointer to pointer of strings) with NULL pointer at the end of created array or only NULL when there are no words in the text.

I already created this function, which works when I test it in c-lion compiler and display words correctly, but when I test it with testing program on classes, it throw me an error that the function returns NULL instead of pointer to allocated memory.

char** split_words(const char *text){
    if (text == NULL) return NULL;

    //fun() to count words in given text
    int words_number = count_words(text);

    //allocate memory for result array, size: words_number * pointer for each word
    char **result = (char**)malloc((words_number + 1) * sizeof(char*));
    if (result == NULL){
        return NULL;
    }

    //temporary word variable to save wrods in result array
    char *word = (char*)malloc(50 * sizeof(char));
    if (word == NULL){
        destroy(result);
        return NULL;
    }

    int i = 0, next_word=0;
    while (i < (int)strlen(text)){
        while (i < (int)strlen(text)){
            if ((*(text + i) >= 'a' && *(text + i) <= 'z') || (*(text + i) >= 'A' && *(text + i) <= 'Z')) break;
            i++;
        }
        int j = 0;
        while (i < (int)strlen(text)){
            if (strchr(" '.,{}[]/\\\"-;:", *(text + i)) != NULL) break;
            *(word + j) = *(text + i);
            i++;
            j++;
        }
        if (j > 0){
            // add
            *(word + j) = '\0';
            // allocate memory for each word in result array
            *(result + next_word) = (char*)malloc((strlen(word) + 1) * sizeof(char));
            if(*(result + next_word) == NULL){
//                destroy(result);
                free(word);
                return NULL;
            }
            strcpy(*(result + next_word), word);
            next_word++;
        }
    }

    // add NULL pointer at the end of words array
    *(result + next_word) = NULL;
    free(word);
    return result;
}

Function passed the test, when given string has no words, and function need to return pointer to NULL.

I'm not sure if this memory allocating is right, in properties of test I've information about wrong access to array out of bound when I try set NULL at the end -> *(result + next_word) = NULL, but I'm confused why it work properly e.g. in c-lion compiler.

Additionaly, here is my destroy function to deallocate memory of result:

void destroy(char **words){
    if (words == NULL) return;

    int i = 0;
    while (*(words + i) != NULL){
        free(*(words + i));
        i++;
    }

    free(words);
}

Thanks for suggestion about using of NULL pointer in double pointer array


[Edit]

the count_words function:

int count_words(const char *text){
    if (text == NULL) return 0;
    int counter = 0, state = 1;

    for (int i=0 ; *(text + i) != '\0'; i++){
        if (strchr(" '.,{}[]/\\\"-;:", *(text + i)) != NULL){
            state = 1;
        } else if (state == 1){
            state = 0;
            counter++;
        }
    }

    return counter;
}
0

There are 0 best solutions below