gpt4 book ai didi

C 编程 : Bag of words program, 需要帮助双指针

转载 作者:行者123 更新时间:2023-11-30 20:59:05 25 4
gpt4 key购买 nike

我正在尝试完成一个单词包程序,但我被困在一个使用双指针的特定函数上。

char *get_word( char **string_ptr )
{
char *word;
int i;

word = malloc(919); /* Assign memory space for storying gathered characters */
i = 0;

for(i = 0; i < strlen(*string_ptr); i++) /* sort through double pointer */
{
if(((**string_ptr) != '\0') && (isalpha(**string_ptr) == 0))
{
*string_ptr += 1;
}
else if((**string_ptr) == '\0')
{
return NULL;
}
}

*string_ptr += strlen(*string_ptr);

return word;
}

该函数被调用到main.c

int main()
{
char *sentence = "#The quick brown fox jumped over 23&%^24 the lazy dogs."; /* test sentence */
char *word; /* pointer to a word */

printf( "sentence = \"%s\"\n", sentence ); /* show the sentence */

while (*sentence) /* while sentence doesn't point to the '\0' character at the end of the string */
{
word = get_word( &sentence ); /* this will allocate memory for a word */
printf( "word = \"%s\"; sentence = \"%s\"\n", word, sentence ); /* print out to see what's happening */

free(word); /* free the memory that was allocated in get_word */
}

return 0;
}

目前我能够打印出该函数:

sentence = "#The quick brown fox jumped over 23&%^24 the lazy dogs."
word = ""; sentence = ""

第一次通话后的句子是:“敏捷的棕色狐狸跳过了 23&%^24 只懒狗。”所以我相信我正确地删除了符号,但我也删除了需要保留的空格。

我正在寻找的最终产品是:

sentence = "#The quick brown fox jumped over 23&%^24 the lazy dogs."
word = "The"; sentence = " quick brown fox jumped over 23&%^24 the lazy dogs."
word = "quick"; sentence = " brown fox jumped over 23&%^24 the lazy dogs."
word = "brown"; sentence = " fox jumped over 23&%^24 the lazy dogs."
word = "fox"; sentence = " jumped over 23&%^24 the lazy dogs."
word = "jumped"; sentence = " over 23&%^24 the lazy dogs."
word = "over"; sentence = " 23&%^24 the lazy dogs."
word = "the"; sentence = " lazy dogs."
word = "lazy"; sentence = " dogs."
word = "dogs"; sentence = "."

word = "(null)"; sentence = ""

很抱歉这篇文章很长。我将不胜感激我能得到的任何帮助。

最佳答案

当前编写的函数存在一些问题。

这是循环字符串的糟糕方法。它在每次迭代结束时重新计算剩余字符串的长度。

for(i = 0; i < strlen(*string_ptr); i++) /* sort through double pointer */

更好的方法是将 strlen() 的结果存储在变量中,或者更好的是,利用字符串总是以 结尾的知识NUL 像这样

for(;**string_ptr!='\0';(*string_ptr)++)

在函数开头为word分配内存是一个坏主意。首先,你不知道你需要多少内存,虽然 919 字节可能就足够了,但你不需要猜测,因为一旦你有了一个单词,你应该知道它有多长。此外,在某些情况下,您的代码会返回 NULL,这意味着分配的内存将丢失。

word = malloc(919); /* Assign memory space for storying gathered characters */

这一行会跳过 string_ptr 到字符串的最末尾,因此您永远不会得到多个结果。

*string_ptr += strlen(*string_ptr); 

完成您想要做的事情的方法是,首先跟踪单词的开头位置,然后找到它的结尾。然后您将确切地知道需要分配多少内存(记住存储 NUL 的额外字节)。这是一个可能的示例,说明如何执行此操作

char *get_word(char **string_ptr)
{
char *start;
char *word=NULL;
unsigned int word_len;

// Return NULL if there is no string to process
if((string_ptr==NULL)||(*string_ptr==NULL)||(**string_ptr=='\0'))
{
return NULL;
}

// Skip over any non-alpha characters
while((**string_ptr!='\0')&&(!isalpha(**string_ptr)))
{
(*string_ptr)++;
}

if(**string_ptr=='\0')
{
return NULL;
}

// Make note of where this word starts
start=*string_ptr;

while(isalpha(**string_ptr))
{
(*string_ptr)++;
}


word_len=(*string_ptr)-start;
if(word_len)
{
word=malloc(word_len+1);
strncpy(word,start,word_len);
word[word_len]='\0'; // Always NUL terminate strings when using strncpy()
}
return word;
}

由于它在某些情况下返回 NULL,因此您还应该确保调用它的代码检查结果是否为 NULL 并采取相应措施,而不是假设它始终为 NULL得到一根绳子。调用 free(NULL) 从来都不是一个好主意。

关于C 编程 : Bag of words program, 需要帮助双指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48710272/

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