【发布时间】:2018-08-07 19:48:44
【问题描述】:
我需要创建一个函数来接收子字符串作为参数,并检查链表中的任何字符串是否包含该子字符串。
该函数创建一个新的链表,其中包含包含给定子字符串的所有字符串。
函数如下所示:
lista *lista_pesquisa_substring(lista *lst, char *substring)
{
l_elemento *aux, *curr;
lista *lis2;
int i = 0, tamanho;
lis2 = lista_nova();
if(lst == NULL || substring == NULL){
return NULL;
}
for (aux = lst->inicio; aux != NULL; aux = aux->proximo)
{
curr = lista_elemento(lis2, i);
if (strstr(*aux->str, substring) != NULL)
{
lista_insere(lis2, aux->str, curr);
i++;
}
}
tamanho = lista_tamanho(lis2);
printf("Foram carregados %d jogos.\n", tamanho);
for(i = 0; i < tamanho; i++)
{
curr = lista_elemento(lis2, i);
printf("Pos %d -> %s\n", i, curr->str);
}
return lis2;
}
if 语句未按预期工作。仅当给定的子字符串与链表中的字符串完全相同时才有效。
例如,如果链表中的字符串是“Hello there”,而给定的子字符串是“Hello there”,则 if 语句有效。 但如果子字符串是“there”,则 if 不起作用。
编辑: 我还应该提到,用作参数的子字符串是用户使用 fgets 函数编写的。
我尝试使用预先存在的字符串来测试它并且它可以工作。所以我很困惑为什么它不适用于用户输入的字符串。
【问题讨论】:
标签: c string list substring strstr