c - 动机的功能搜索给出了大写字母的错误

标签 c

我已经在 C 中实现了这个函数,以在给定文件中查找动机。 因此该文件包含如下消息: 比如ISI, JE SERAI INGENIEURE等都是消息。

19971230 20220512 JE SERAI INGENIEURE
19950511 20211230 ISI
19971230 20301123 INFORMATIQUE C'EST LA VIE
20020809 20081025 LANGAGE DE PROGRAMMATION
20070905 20201104 ECOLE INGENIEUR
19990102 20051025 ORDINATEUR
20081212 20200312 JE SUIS 

每条信息都放在一个链表中。

这是我的代码:

void trouverMotif(message_t ** tete, char motif[])
{
    message_t *temp = *tete;
    char *ptr = motif;
    int i = 0, j = 0;

    while(temp != NULL)
    {
        printf("%s\n",temp->text);
        i = 0;
        j = 0;

        while(temp->text[i] != ptr[j] && temp->text[i] != '\0')
        {
            i++;
        }

        if(temp->text[i] == ptr[j])
        {
            while(temp->text[i] != '\0' && ptr[j] != '\0' && ptr[j] == temp->text[i])
            {
                j++;
                i++;
            }

            if(ptr[j] == '\0')
            {
                printf("%s", temp->text);
            }
        }
        temp = temp->suivant;
    }
}

所以我想要的结果是,如果我输入 motif = "INGE",在我的例子中,结果会给我包含“INGE”的消息; JE SERAI INGENIEURE 和 ECOLE INGENIEUR。

问题是我获得的结果只是 ECOLE INGENIEUR 而不是两者。当我将消息更改为小写字母时,它给出了正确的结果(两者)。这怎么可能?

最佳答案

代码

   while(temp->text[i] != ptr[j] && temp->text[i] != '\0')
    {
        i++;
    }

    if(temp->text[i] == ptr[j])
    {
        while(temp->text[i] != '\0' && ptr[j] != '\0' && ptr[j] == temp->text[i])
        {
            j++;
            i++;
        }

        if(ptr[j] == '\0')
        {
            printf("%s", temp->text);
        }
    }

如果ptr 的第一个字符第一次出现在temp->text 中不是您搜索的序列,则失败

例如,如果 ptr 是 "AZE"并且 temp->text 是 "...A AZE"你的第一个 while set i到 3,并且在比较“AZE”和“A AZE”之后发现不相等


这里有一个建议,代码必须检查 motif 是否是每个消息文本的子字符串,所以更自然的是定义 contient 等同于 strstr :

char * contient(const char * meule, const char * aiguille)
{
  /* strstr */
  while (*meule) {
    const char * p1 = meule;
    const char * p2 = aiguille;

    if (*p1 == *p2) {
      do {
        p1 += 1;
        p2 += 1;

        if (*p2 == 0)
          return (char *) meule;
      } while  (*p1 == *p2);
    }

    meule += 1;
  }

  return NULL;
}

void trouverMotif(message_t ** tete, char motif[])
{
  message_t *temp = *tete;

  while(temp != NULL)
  {
    /* printf("%s\n", p); */

    if (contient(temp->text, motif) != NULL)
      printf("'%s' found in '%s'\n", motif, temp->text);

    temp = temp->suivant;
  }
}

例如,如果我添加:

typedef struct message_t {
  char * text;
  struct message_t * suivant;
}  message_t;

message_t * mk(char * t, message_t * n)
{
  message_t * r = malloc(sizeof(message_t));

  r->text = t;
  r->suivant = n;
  return r;
}

int main()
{
  message_t * tete = mk("19971230 20220512 JE SERAI INGENIEURE",
                        mk("19950511 20211230 ISI",
                           mk("19971230 20301123 INFORMATIQUE C'EST LA VIE",
                              mk("20020809 20081025 LANGAGE DE PROGRAMMATION",
                                 mk("20070905 20201104 ECOLE INGENIEUR",
                                    mk("19990102 20051025 ORDINATEUR",
                                       mk("20081212 20200312 JE SUIS ", NULL)))))));


  trouverMotif(&tete, "INGE");
}

编译和执行:

pi@raspberrypi:/tmp $ ./a.out
'INGE' found in '19971230 20220512 JE SERAI INGENIEURE
'INGE' found in '20070905 20201104 ECOLE INGENIEUR

备注:你不需要 trouverMotif(message_t ** tete, char motif[]) 因为你不修改 tete,所以 trouverMotif(message_t * tete, char motif[]) 就够了

关于c - 动机的功能搜索给出了大写字母的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55127521/

相关文章:

c - 从文件中删除字节 - c

c -/usr/bin/ld : cannot find -ll

C 指针操作

c - printf ("\033c") 是什么意思?

c - 使用不同功能时出现段错误

c - 链接列表不显示

c - GCC 为 max 宏推荐了一个奇怪的实现

c - 字节级长度描述

c - 为什么在传递参数时使用 const 会给我一个警告?

c - 高科技 C 编译器位结构