c 在字符串中搜索目标字符串

比斯拉努尔欧洲经委会

编写一个 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)
            ^~~~~~
j23

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

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]指字符串中的一个字符。所以给 as &array[i][length-2]inside strcmp 给出倒数第二个元素的地址。strlen. 另外)不匹配是抛出too few arguments错误if(strcmp((array[i][length-2],"ed")==0)(它很琐碎,有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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章