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

标签 c arrays string

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

最佳答案

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

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

错误:变量长度未声明。所以声明它(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 中给出 &array[i][length-2] 来给出倒数第二个元素的地址。与 strlen 的情况相同。另外, ) 不匹配会在 if(strcmp((array[i][length-2],"ed")==0) 中引发 too less argument 错误 (非常简单,有 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;
}

关于c 在字符串中搜索目标字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57626952/

相关文章:

string - 如何根据要求对字符串进行分段

c - 理解C中的解引用运算符和指针,以及一个小程序的逻辑

c - 两个子进程通过命名管道进行通信

c - 初始化大型双数组时出现问题

python - 增量构建 numpy 数组的最佳方法是什么?

c - 如何在c中分割字符串并将每个字符放入数组中?

c - ']' token 之前的预期表达式?

c - 如何在不更改其他位的情况下更改 32 位寄存器的特定位?

php - lotto.php(我正在努力将随机数放入 "ticket")

string - 除了 Knuth-Morris-Pratt、Rabin-Karp 之类的,还有哪些可用的字符串匹配算法?