【问题标题】:c search for target string in string [closed]c在字符串中搜索目标字符串[关闭]
【发布时间】:2019-08-23 13:05:51
【问题描述】:

编写一个 C 程序,读取一系列的字符串并仅打印那些以字母“ed”结尾的字符串。

我的代码:

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

int main(){

    char array[5][10];
    int i;
    for(i=0;i<5;i++){
        printf("%s","enter a string");//enter string
        scanf("%10s",&array[i][0]);
    }

    char *array[5][0];
    for(i=0;i<=4;i++){
        length=strlen(array[i][0]);// 
        if(strcmp((array[i][length-2],"ed")==0) //I wrote to make a comparison//
        {
            printf("%s\n",&array[i][0]);
        }
    }
    return 0;
}

错误:

 extern.c:14:7:conflicting types for ‘array’
 char *array[5][0];
       ^~~~~
extern.c:6:6: note: previous declaration of ‘array’ was here
 char array[5][10];
      ^~~~~
extern.c:16:1: error: ‘length’ undeclared (first use in this function)
 length=strlen(array[i][0]);
 ^~~~~~
extern.c:16:1: note: each undeclared identifier is reported only once for each function it appears in
extern.c:17:11: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
 if(strcmp((array[i][length-2],"ed")==0)
           ^
In file included from extern.c:2:0:
/usr/include/string.h:136:12: note: expected ‘const char *’ but argument is of type ‘int’
 extern int strcmp (const char *__s1, const char *__s2)
            ^~~~~~
extern.c:17:4: error: too few arguments to function ‘strcmp’
 if(strcmp((array[i][length-2],"ed")==0)
    ^~~~~~
In file included from extern.c:2:0:
/usr/include/string.h:136:12: note: declared here
 extern int strcmp (const char *__s1, const char *__s2)
            ^~~~~~

【问题讨论】:

  • 您已经声明了array。第二个是不同的类型。
  • 不要将地址运算符&amp; 用于printf。编译器已经传递了地址。
  • 删除第二个声明和&amp;,代码应该可以运行了。
  • ...而且有一个( 太多在调用strcmp
  • “'array' 的冲突类型”是不言自明的。

标签: c arrays string


【解决方案1】:

查看错误消息足以调试代码并使其正常工作。这是非常基本的错误消息,只需阅读一次代码即可解决。

1)

extern.c:14:7:conflicting types for ‘array’
 char *array[5][0];
       ^~~~~
extern.c:6:6: note: previous declaration of ‘array’ was here
 char array[5][10];

错误:同一变量声明了两次。所以删除一个声明(第 14 行)。

2)

      ^~~~~
extern.c:16:1: error: ‘length’ undeclared (first use in this function)
 length=strlen(array[i][0]);
 ^~~~~~
extern.c:16:1: note: each undeclared identifier is reported only once for each function it appears in

错误:变量length 未声明。所以声明它(int length

3)

extern.c:17:11: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
 if(strcmp((array[i][length-2],"ed")==0)
           ^
In file included from extern.c:2:0:

/usr/include/string.h:136:12: note: expected ‘const char *’ but argument is of type ‘int’
 extern int strcmp (const char *__s1, const char *__s2)
            ^~~~~~
extern.c:17:4: error: too few arguments to function ‘strcmp’
 if(strcmp((array[i][length-2],"ed")==0)
    ^~~~~~
In file included from extern.c:2:0:
/usr/include/string.h:136:12: note: declared here
 extern int strcmp (const char *__s1, const char *__s2)

很明显 strcmp 期望 const char * 但你给出的是整数。 array[i][length-2] 指的是字符串中的一个字符。所以在 strcmp 中给出 &amp;array[i][length-2] 以给出倒数第二个元素的地址。与strlen 的情况相同。还有) mismatch 在if(strcmp((array[i][length-2],"ed")==0) 中抛出too few arguments 错误(非常简单,有3 个( 和2 个),只需查看代码) .

4) 程序中还有}不匹配。

所以最终在解决错误之后,它应该看起来像这样:

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

int main()
{
    char array[5][10];
    int i;

    for( i = 0; i < 5; i++ ) 
    {
        printf("%s", "enter a string"); //enter string
        scanf("%10s", array[i]);
    }
    for( i = 0; i < 5; i++ )
    {
        size_t length = strlen(array[i]);
        if(strcmp(&array[i][length-2], "ed") == 0)
        {
            printf("%s\n",array[i]);
        }
    }

    return 0;
}

【讨论】:

  • size_t length; 会更好。
  • 非常感谢我的代码正常工作:)))
猜你喜欢
  • 2020-07-27
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多