Tal Zichlinsky
Tal Zichlinsky

Reputation: 60

How to split a text into two dimensional array in c

I'm trying to split this string:

this is a text file
looking for the word cat
the program should print also cats
and crat and lcat but it shouldn’t
print the word caats

into a two dimensional arrays such that every line in the text is a line in the array.
For example:

lines[0][0] = 't'
lines[0][1] = 'h'

and so on. For now, this is my code:

void print_lines(char txt[]){
    char lines[SIZE][SIZE];
    int num_of_lines = fill_lines(txt, lines);
    printf("lines: %d\n",num_of_lines );
    int i;
    for (i = 0; i < num_of_lines; i++)
    {   
        printf("%s\n", lines[i]);
    }
}

int fill_lines(char txt[], char lines[][]){
        char copy[strlen(txt)];
        memcpy(copy, txt, strlen(txt));
        char *line = strtok(copy, "\n");

        int i = 0;
        while(line != NULL){
            strcpy(lines[i][0], line);
            line = strtok(NULL, "\n");
            i++
        }
    return i + 1;
}

The problem I'm currently dealing with is an error in strcpy(lines[i], line) that reads:

expression must be a pointer to a complete object type

I have also tried memcpy(lines[i], line, strlen(line)).

Any help would be much appreciated.

Upvotes: 1

Views: 1388

Answers (1)

Ismail El Moudni
Ismail El Moudni

Reputation: 711

I think this should work for you Here I used '\n' as a delimiter

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

char **str_split(char *a_str, const char a_delim)
{
    char **result = 0;
    size_t count = 0;
    char *tmp = a_str;
    char *last_comma = 0;
    char delim[2];
    delim[0] = a_delim;
    delim[1] = 0;

    /* Count how many elements will be extracted. */
    while (*tmp)
    {
        if (a_delim == *tmp)
        {
            count++;
            last_comma = tmp;
        }
        tmp++;
    }

    /* Add space for trailing token. */
    count += last_comma < (a_str + strlen(a_str) - 1);

    /* Add space for terminating null string so caller
       knows where the list of returned strings ends. */
    count++;

    result = malloc(sizeof(char *) * count);

    if (result)
    {
        size_t idx = 0;
        char *token = strtok(a_str, delim);

        while (token)
        {
            assert(idx < count);
            *(result + idx++) = strdup(token);
            token = strtok(0, delim);
        }
        assert(idx == count - 1);
        *(result + idx) = 0;
    }

    return result;
}

int main()
{
    char text[] = "this is a text file\nlooking for the word cat\nthe program should print also cats\nand crat and lcat but it shouldn’t\nprint the word caats";
    char **tokens;

    printf("ORIGINAL TEXT:\n%s\n\n", text);

    tokens = str_split(text, ',');

    if (tokens)
    {
        int i;
        for (i = 0; *(tokens + i); i++)
        {
            printf("%s\n", *(tokens + i));
            free(*(tokens + i));
        }
        printf("\n");
        free(tokens);
    }

    return 0;
}

Upvotes: 1

Related Questions