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("%sn",&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)
^~~~~~

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

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 中给出 as&array[i][length-2]以给出倒数第二个元素的地址。与strlen相同。此外)不匹配是在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("%sn",array[i]);
}
}
return 0;
}

最新更新