一个单词表存放了五个表示颜色的英文单词,输入一个字母,在单词表中查找并输出所有以此字母开头的单词,若没有找到,输出Not Found。要求用指针数组和二级指针实现。

#include
int main(void)
{
int i, flag = 0;
const char< 填空 > ;

   pc = color;
   ch = getchar();
   for(i = 0; i < 5; i++) {
        if(<        填空       >(pc + i) == ch){
           flag = 1;
           puts(<        填空        >(pc + i) );
        }
   }
   if(flag == 0) {
     printf("Not Found\n"); 
  }    

   return 0;

}

因为你的第一个填空的地方,声明的是const char,而下面ch没有声明,所以,第一个填空的地方需要用分号来分隔声明,填空如下:
(1)

** pc, * color[5] = { "black","white","green","yellow","blue" }; char ch;

(2)填2个星号


**

(3)只有一个星号


*

运行结果:

img

完整代码:

#include<stdio.h>
int main(void)
{
    int i, flag = 0;
    const char** pc, * color[5] = { "black","white","green","yellow","blue" }; char ch;

    pc = color;
    ch = getchar();
    for (i = 0; i < 5; i++) {
        if (**(pc + i) == ch) {
            flag = 1;
            puts(*(pc + i));
        }
    }
    if (flag == 0) {
        printf("Not Found\n");
    }

    return 0;
}

#include<stdio.h>
int main(void)
{
int i, flag = 0;
const char pc, color[] = {“green”, “blue”, “yellow”, “red”, “white”};
// 将color字符串数组绑定到指针数组pc上
pc = color;
// 读入待查找的字母
char ch = getchar();
for(i = 0; i < 5; i++) {
// 在每个字符串中检查是否以该字母开头
if((pc + i) == ch){
flag = 1;
// 如果是则输出该字符串
puts((pc + i));
}
}
if(flag == 0) {
printf(“Not Found\n”);
}

return 0;
}

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 以下回答来自chatgpt:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char *color[] = {"red", "green", "blue", "yellow", "black"}; // 定义颜色单词表
        char c;
        scanf("%c", &c); // 输入首字母
        char **p[5];
        int cnt = 0;
        for (int i = 0; i < 5; i++) {
            if (color[i][0] == c) { // 判断首字母
                p[cnt++] = &color[i]; // 记录符合条件的单词的指针地址
            }
        }
        if (cnt == 0) { // 如果没有符合条件的单词
            printf("Not Found\n");
        }
        else {
            for (int i = 0; i < cnt; i++) { // 输出所有符合条件的单词
                printf("%s\n", *p[i]);
            }
        }
        return 0;
    }
    

    解析:

    题目要求在一个存放了五个颜色单词的单词表中查找所有以输入的字母开头的单词,并输出结果。

    我们可以先定义一个包含这五个颜色单词的字符串数组,每个单词用一个 char* 指针指向开始位置。

    接着,我们可以输入一个首字母,然后遍历这个字符串数组,找到所有以这个字母开头的单词,记录下这些单词的指针地址。

    最后,再判断一下是否有符合条件的单词即可,如果有,就输出所有符合条件的单词,否则输出 "Not Found"。

    具体实现过程中使用一个指针数组和双重指针来实现,第一个指针数组 p 存放符合条件的单词的指针地址,第二个双重指针指向颜色单词表,用于遍历字符串数组。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

这么填,供参考:

#include<stdio.h>
int main(void)
{
    int i, flag = 0;
    const char **pc,*color[]={"green", "blue", "yellow", "red", "white"}; //< 填空 > ;

    pc = color;
    char ch = getchar(); //这行题目里漏了 char ?
    for(i = 0; i < 5; i++) {
        if(*(pc[i]) == ch){//< 填空 > 也可填:if(*(*(pc + i)) == ch)
           flag = 1;
           puts(pc[i]);//< 填空 > 也可填 puts(*(pc + i));
        }
    }
    if(flag == 0) {
         printf("Not Found\n");
    }
    return 0;
}