gpt4 book ai didi

C 语言 -> 将段落中的单词分开

转载 作者:行者123 更新时间:2023-11-30 16:38:28 25 4
gpt4 key购买 nike

我的函数 save_words 接收 Armazena 和大小。 Armazena 是一个包含段落的动态数组,size 是数组的大小。在这个函数中,我想将单词放入其他称为单词的动态数组中。当我运行它时,它崩溃了。我感谢您的帮助。

char **save_words(char **armazena, int *size)
{
char *token = NULL;
char** armazena_aux = armazena;
int i, count=0;
char **words = (char**) malloc(sizeof(char*)*(10));
for(i=0; i<size; i++)
{
token = strtok(*(armazena+i)," .?!,");
while( token != NULL )
{
int tam = strlen(token);
armazena[count] = (char*) malloc(tam+2);
strcpy(armazena[count],token);
armazena[count][tam+1]='\0';
count++;
token = strtok(NULL, " .?!,");

if (count%10==0)
{
words = realloc(words, sizeof(char*)*(count + 10));
}
}
}
return words;
}

最佳答案

armazena[count] = (char*) malloc(tam+2); 是你想要的吗?我本以为words[count] = ...;。第一次通过外循环是可以的,因为你将 armazena[0] 提升到 strtok 中,但如果它包含多个单词,第二次通过外循环将处理第一次生成的字符串。更糟糕的是,如果第一个字符串包含的单词数量超过了 armazena vector 所能容纳的数量,那么您将破坏某些内容......

关于C 语言 -> 将段落中的单词分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47474024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com