Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
75 views
in Technique[技术] by (71.8m points)

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
",num_of_lines );
    int i;
    for (i = 0; i < num_of_lines; i++)
    {   
        printf("%s
", lines[i]);
    }
}

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

        int i = 0;
        while(line != NULL){
            strcpy(lines[i][0], line);
            line = strtok(NULL, "
");
            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.

question from:https://stackoverflow.com/questions/65541424/how-to-split-a-text-into-two-dimensional-array-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this should work for you Here I used ' ' 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
looking for the word cat
the program should print also cats
and crat and lcat but it shouldn’t
print the word caats";
    char **tokens;

    printf("ORIGINAL TEXT:
%s

", text);

    tokens = str_split(text, ',');

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

    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...