使用子字符串检查字符串是否存在(使用链表)
Checking if a string exists using a substring (using linked lists)
我需要创建一个函数来接收子字符串作为参数,并检查链表中的任何字符串是否包含该子字符串。
该函数创建一个新的链表,其中包含包含给定子字符串的所有字符串。
函数如下所示:
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 函数编写的。
我尝试使用预先存在的字符串来测试它并且它有效。所以我很困惑为什么它不适用于用户输入的字符串。
I should also mention that the substring used as an argument is wrote by the user using the fgets function.....
So I'm confused as to why it doesn't work with a string that's input by the user.
当您使用 fgets()
函数从用户那里获取输入时它不起作用的一个可能原因是当用户输入并按下 ENTER
键时,fgets()
考虑换行字符作为有效字符并将其包含在用户给出的输入字符串中。因此,如果用户输入字符串 "there"
并按下 Enter
键,fgets()
从输入流中读取字符并将它们存储在缓冲区中,如下所示:
+---------------------------+
| t | h | e | r | e | \n| [=10=]|
+---------------------------+
^^
在将其传递给在链表中查找子字符串的函数之前,从输入缓冲区中删除尾随的换行符:
inbuffer[strcspn(inbuffer, "\n")] = 0; //inbuffer is buffer which holds input string given by user
我需要创建一个函数来接收子字符串作为参数,并检查链表中的任何字符串是否包含该子字符串。
该函数创建一个新的链表,其中包含包含给定子字符串的所有字符串。
函数如下所示:
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 函数编写的。
我尝试使用预先存在的字符串来测试它并且它有效。所以我很困惑为什么它不适用于用户输入的字符串。
I should also mention that the substring used as an argument is wrote by the user using the fgets function.....
So I'm confused as to why it doesn't work with a string that's input by the user.
当您使用 fgets()
函数从用户那里获取输入时它不起作用的一个可能原因是当用户输入并按下 ENTER
键时,fgets()
考虑换行字符作为有效字符并将其包含在用户给出的输入字符串中。因此,如果用户输入字符串 "there"
并按下 Enter
键,fgets()
从输入流中读取字符并将它们存储在缓冲区中,如下所示:
+---------------------------+
| t | h | e | r | e | \n| [=10=]|
+---------------------------+
^^
在将其传递给在链表中查找子字符串的函数之前,从输入缓冲区中删除尾随的换行符:
inbuffer[strcspn(inbuffer, "\n")] = 0; //inbuffer is buffer which holds input string given by user