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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…