【问题标题】:Stuck with comparing Strings in C [closed]坚持比较C中的字符串[关闭]
【发布时间】:2018-01-24 16:55:21
【问题描述】:

所以我应该编写一个 Soundex 转换器代码并打印文件中的行,如果其中一个单词与输入具有相同的 Soundex 代码。我成功地为 Soundex 转换编写了一个函数,但我停留在比较部分。抱歉,如果这听起来微不足道,但是当我逐行比较单词时,strcmp 似乎每次都失败。这是代码...抱歉,如果它太长了

    #include<string.h>
    #include<ctype.h>
    #include<stdio.h>
    #include<stdlib.h>

    char *soundex(char *s,char* name)
    {
        int si = 1;
        char c;
        //char *s = (char *)malloc(1000);
        //                 ABCDEFGHIJKLMNOPQRSTUVWXYZ
        char mappings[] = "01230120022455012623010202";

        s[0] = toupper(name[0]);

        for(int i = 1, l = strlen(name); i < l; i++)
        {
            c = toupper(name[i]) - 65;

            if(c >= 0 && c <= 25)
            {
                if(mappings[c] != '0')
                {
                    if(mappings[c] != s[si-1])
                    {
                        s[si] = mappings[c];
                        si++;
                    }

                    if(si > 3)
                    {
                        break;
                    }
                }
            }
        }

        if(si <= 3)
        {
            while(si <= 3)
            {
                s[si] = '0';
                si++;
            }
        }

        //printf("%s\n",s);
        return s;

    }

    void search(char line[10000],char str[1000])
    {   
        int i,j=0;
        char test[1000];
        char s[1000];
        char b[1000];
        for(i=0;line[i] != '\0';i++)
        {   
            if(line[i] == ' ')
            continue;

            test[j] = line[i];
            j++;
            if(line[i+1] == ' ' || line[i+1] == '\0')
            {
                //soundex(test);
                test[j] = '\0';

                if(strcmp(soundex(s,test),soundex(b,str)) == 0);
                {
                    printf("%s\n",soundex(s,test));
                    printf("%s\n",soundex(b,str));
                    printf("%s",line);
                    break;
                }
                j = 0;
                memset(test,0,strlen(test));
            }

        }
    }

    int main()
    {

        char a[1000],f[1000];
        char s[1000];
        gets(a);
        //soundex(s,a);
        //printf("%s",s);

        scanf("%s",f);
        FILE *fp=fopen(f,"r");
        if(fp==NULL)
        {
            printf("File doesnot exist bro");
        }
        else
        {
            long long linenum=1;
            char line[10000];
            while(fgets(line,10000,fp)!=NULL) //Or fscanf
            {
                search(line,a);
                //printf("%s",line);
                linenum++;
            }
        }

        fclose(fp);
     }

搜索功能中的 strcmp 命令会出现问题,因为即使结果不同,它也会打印行。我什至会在之后打印比较结果来确定。任何潜在客户都会受到重视。再次对长代码感到抱歉。

【问题讨论】:

  • 这是一个需要先调试解决的问题。尝试调试它,如果卡住了再回来。
  • 下次尝试创建minimal reproducible example
  • 此代码也出现编译器警告,请先修复这些警告。 ` test[j] == '\0';` for 1
  • edit你的问题告诉我们你做了什么样的调试。我希望您已经在 Valgrind 或类似的检查器中运行了您的minimal reproducible example,并使用诸如 GDB 之类的调试器进行了调查。确保您也启用了全套编译器警告。这些工具告诉了你什么,它们缺少什么信息?并阅读 Eric Lippert 的 How to debug small programs

标签: c string file


【解决方案1】:

关闭;不要忽略编译器警告!

prog.c:70:17: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
                     if(strcmp(soundex(s,test),soundex(b,str)) == 0);


if(strcmp(soundex(s,test),soundex(b,str)) == 0)

还有

prog.c: In function ‘search’:
prog.c:68:25: warning: statement with no effect [-Wunused-value]
                 test[j] == '\0';

prog.c: In function ‘main’:
prog.c:89:9: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
         gets(a);

【讨论】:

  • 哇,真的花了 30 分钟。谢谢,它没有在 Dev C++ 上显示为编译器警告
  • 尝试提高警告级别(或使用更好的编译器)
  • @Reddy90 :Dev-C++ 不是编译器,它是 IDE,并且显示的警告只会在答案中指定的警告选项中给出 - 尽管即使在发布的 GCC 版本中也可能没有使用 Dev-C++。即使这样,问题也可以通过更好的编码风格和调试器来解决——例如,不要在if 中调用函数并在调试器中检查它的返回值——然后你会看到strcmp() 有效,问题出在如果。
  • 您应该使用 Orwell Dev-C++ 而不是(过时的)旧版本。 orwelldevcpp.blogspot.com
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-21
  • 1970-01-01
相关资源
最近更新 更多