为什么我的程序为模式匹配程序给出错误的输出?

时间:2019-02-02 09:57:22

标签: c algorithm pattern-matching

我用'c'语言编写了一个模式匹配程序。但是它总是向我显示错误的输出。使用相同的逻辑,我已经用'c ++'语言编写了代码,并且在那里工作得很好,但在'c'中却没有。

根据我的程序,首先我需要输入要输入的“文本”和“样式”的大小。然后根据大小,字符数组应使用for循环在其中存储相应数量的字符。但对于前。如果我输入的文字大小为5,则它仅存储2个字符,然后进一步显示错误的答案。

#include<stdio.h>
int p, t, c, LOC, i, j;

void main()

{

printf("Enter size of Text and Pattern\n");

scanf("%d %d",&t,&p);

char pat[p];

char txt[t];

    printf("Enter the Text\n");
    for(i=0;i<t;i++)
        {
            scanf("%c",&txt[i]);
        }
    printf("Enter the Pattern\n");
    for(i=0;i<p;i++)
        {
            scanf("%c",&pat[i]);
        }
    int MAX=t-p+1;


    for(i=0;i<MAX;i++)
    {
        int count=0;
        c=0;
           for(j=i;j<i+p;j++)
            {
                if(pat[count]==txt[j])
                {
                    count++;
                    c++;
                }
                else
                    break;
            }
           if(c==p)
            {

                LOC=i+1;
                break;
            }
    }
    if(LOC!=0)
        printf("Pattern found at location: %d",LOC);
    else
       printf("NOT FOUND\n");
}

预期:

Enter size of Text and Pattern

5 2

Enter the Text

abbca

Enter the Pattern

bc

Pattern found at location: 3

实际:

Enter size of Text and Pattern

5 2

Enter the Text

abb

Enter the Pattern

a

NOT FOUND

2 个答案:

答案 0 :(得分:0)

for(i=0;i<t;i++)
    {
        scanf("%c",&txt[i]);
    }

如果Enter the text时输入的字符过多,则表示字符串中的 t 个字符更多,多余的字符将用于模式。请注意,\ n会在其中被读取

在其他元素之后读取字符串/字符时不会出现此问题,我建议您每次都读取一行。

警告:pat和txt的大小必须再增加一个,以便能够记住终止的空字符

您可以这样做:

int main()
{
  char  *line;
  size_t n;

  printf("Enter size of Text and Pattern\n");
  line = 0;
  n = 0;
  if (getline(&line, &n, stdin) == -1) {
    puts("abort");
    return -1;
  }
  if (sscanf(line, "%d %d",&t,&p) != 2) {
    puts("invalid size of Text and Pattern");
    return -1;
  }
  free(line);

  char pat[p+1]; /* warning +1 */
  char txt[t+1]; /* warning +1 */
  char fmt[16];

  printf("Enter the Text\n"); 
  line = 0;
  n = 0;
  if (getline(&line, &n, stdin) == -1) {
    puts("abort");
    return -1;
  }

  sprintf(fmt, "%%%ds", t); /* makes the right format */
  if ((sscanf(line, fmt, txt) != 1) || (strlen(txt) != t)) {
    puts("txt is too small");
    return -1;
  }
  free(line);

  printf("Enter the Pattern\n");
  line = 0;
  n = 0;
  if (getline(&line, &n, stdin) == -1) {
    puts("abort");
    return -1;
  }
  sprintf(fmt, "%%%ds", p); /* makes the right format */
  if ((sscanf(line, fmt, pat) != 1) || (strlen(pat) != p)) {
    puts("pat is too small");
    return -1;
  }
  free(line);

  int MAX=t-p+1;
  ...

执行:

Enter size of Text and Pattern
5 2
Enter the Text
abbca
Enter the Pattern
bc
Pattern found at location: 3

valgrind 下:

pi@raspberrypi:/tmp $ valgrind ./a.out
==5185== Memcheck, a memory error detector
==5185== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5185== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==5185== Command: ./a.out
==5185== 
Enter size of Text and Pattern
5 2
Enter the Text
abbca
Enter the Pattern
bc
Pattern found at location: 3==5185== 
==5185== HEAP SUMMARY:
==5185==     in use at exit: 0 bytes in 0 blocks
==5185==   total heap usage: 5 allocs, 5 frees, 2,408 bytes allocated
==5185== 
==5185== All heap blocks were freed -- no leaks are possible
==5185== 
==5185== For counts of detected and suppressed errors, rerun with: -v
==5185== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)

注意:

  • 打印位置后最好添加\ n
  • 您使程序变得复杂,要求在读取文本和模式之前先读取它们的大小,而无需在读取之前知道它们的大小,只需像 getline 这样在堆中使用分配的内存即可而不是使用编译时未知的大小将它们放入堆栈中

答案 1 :(得分:-1)

由于要输入,您使用scanf(%c)捕获了额外的\ n

#include<stdio.h>
int p, t, c, LOC, i, j;
char dummy;

void main()

{

printf("Enter size of Text and Pattern\n");

scanf("%d %d",&t,&p);
scanf("%c", &dummy);

char pat[p];

char txt[t];

printf("Enter the Text\n");
for(i=0;i<t;i++)
    {
        scanf("%c",&txt[i]);
    }

scanf("%c", &dummy);
printf("Enter the Pattern\n");
for(i=0;i<p;i++)
    {
        scanf("%c",&pat[i]);
    }
int MAX=t-p+1;


for(i=0;i<MAX;i++)
{
    int count=0;
    c=0;
    for(j=i;j<i+p;j++)
        {
            if(pat[count]==txt[j])
            {
                count++;
                c++;
            }
            else
                break;
        }
    if(c==p)
        {

            LOC=i+1;
            break;
        }
}
if(LOC!=0)
    printf("Pattern found at location: %d\n",LOC);
else
printf("NOT FOUND\n");
}

检查是否有效