• LQ0220 前缀判断【程序填空】


    题目来源:蓝桥杯2013初赛 C++ A组E题

    题目描述
    本题为代码补全填空题,请将题目中给出的源代码补全,并复制到右侧代码框中,选择对应的编译语言(C/Java)后进行提交。若题目中给出的源代码语言不唯一,则只需选择其一进行补全提交即可。复制后需将源代码中填空部分的下划线删掉,填上你的答案。提交后若未能通过,除考虑填空部分出错外,还需注意是否因在复制后有改动非填空部分产生错误。

    如下的代码判断 needle_start 指向的串是否为 haystack_start 指向的串的前缀,如不是,则返回NULL。

    比如:“abcd1234” 就包含了 “abc” 为前缀。

    源代码
    C

    #include 
    
    char* start_with(char* haystack_start, char* needle_start)
    {
        char* haystack = haystack_start;
        char* needle = needle_start;
    
        
        while(*haystack && *needle){
            if(__________________) return NULL;
        }
        
        if(*needle) return NULL;
        
        return haystack_start;
    }
    
    void test(char* a, char* b)
    {
        char* p = start_with(a,b); 
        if(p==NULL) 
            printf("[NO]\n");
        else 
            printf("|%s|\n", p);
    }
    
    int main()
    {
        test("abcd","abc");
        test("abcd","acb");
        test("abcd","abcd");
        test("abcd","");
        test("","abc");
        test("","");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    问题分析
    填入“(*haystack++)!=(*needle++)”

    AC的C语言程序如下:

    #include 
    
    char* start_with(char* haystack_start, char* needle_start)
    {
        char* haystack = haystack_start;
        char* needle = needle_start;
    
        
        while(*haystack && *needle){
            if((*haystack++)!=(*needle++)) return NULL;
        }
        
        if(*needle) return NULL;
        
        return haystack_start;
    }
    
    void test(char* a, char* b)
    {
        char* p = start_with(a,b); 
        if(p==NULL) 
            printf("[NO]\n");
        else 
            printf("|%s|\n", p);
    }
    
    int main()
    {
        test("abcd","abc");
        test("abcd","acb");
        test("abcd","abcd");
        test("abcd","");
        test("","abc");
        test("","");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
  • 相关阅读:
    用Arduino测试ADXL335加速度计如何工作?
    usb hid协议简明简介及使用
    互联网摸鱼日报(2023-05-27)
    开源驰骋低代码-积极拥抱AI时代
    GBase 8c管理平台——3.管理控制平台GBase 8c AMT
    HuggingFace 国内下载 阿里云盘下载速度20MB/s
    spfa求解图中是否含有负环 C++实现
    京东API接口大全
    java基于SpringBoot+Vue的高校招生管理系统 element 前后端分离
    安全生产管理系统——特殊作业管控
  • 原文地址:https://blog.csdn.net/tigerisland45/article/details/127975932