不调用C++/C的字符串库函数,编写函数strcmp

函数功能

模拟标准库 strcmp 功能,比较两个字符串 str1str2,不调用任何 C/C++ 字符串库函数(如 strlen 等),仅通过指针操作实现。

核心逻辑

  1. 同时遍历两个字符串,逐字符对比 ASCII 码值;
  2. 若对应字符不相等,直接返回两字符 ASCII 码差值(str1[i] - str2[i]);
  3. 若遍历至某一字符串末尾(遇到 '\0'),返回两字符串长度差值(本质是剩余字符 ASCII 码差值,因 '\0' ASCII 码为 0);
  4. 若两字符串完全遍历结束且所有字符相等,返回 0。

代码实现

c<stddef.h>

cpp 复制代码
// 比较str1和str2,返回值规则同标准strcmp:
// 正数:str1 > str2;< str2;0:str1 == str2
int my_strcmp(const char* str1, const char* str2) {
    // 校验空指针(标准strcmp对空指针未定义,此处补充容错)
    if (str1 == NULL && str2 == NULL) return 0;
    if (str1 == NULL) return -1;
    if (str2 == NULL) return 1;

    // 逐字符对比,未到字符串末尾且字符相等则继续遍历
    while (*str1 != '\0' && *str2 != '\0' && *str1 == *str2) {
        str1++;  // 指针后移,指向下一字符
        str2++;
    }

    // 返回对应位置字符ASCII码差值(涵盖长度不同场景)
    return *str1 - *str2;
}

测试场景说明
覆盖 6 种核心场景,全面验证 my_strcmp 功能正确性,与标准 strcmp 结果对比(仅用于验证,核心逻辑无库函数依赖):

1. 场景 1:两个字符串完全相等

2. 场景 2:str1 长度大于 str2,前序字符相等

3. 场景 3:str2 长度大于 str1,前序字符相等

4. 场景 4:对应位置字符 str1 ASCII 码大于 str2

5. 场景 5:对应位置字符 str2 ASCII 码大于 str1

6. 场景 6:空指针场景(两个空、str1 空、str2 空)

完整测试代码


#include <stddef.h>
#include <stdio.h>
#include <string.h>  // 仅用于标准strcmp对比验证,非核心逻辑依赖

// 自定义strcmp函数(无库函数依赖)
int my_strcmp(const char* str1, const char* str2) {
    if (str1 == NULL && str2 == NULL) return 0;
    if (str1 == NULL) return -1;
    if (str2 == NULL) return 1;

    while (*str1 != '\0' && *str2 != '\0' && *str1 == *str2) {
        str1++;
        str2++;
    }

    return *str1 - *str2;
}

// 测试工具函数:打印测试结果
void test_my_strcmp(const char* str1, const char* str2, const char* scene) {
    int my_result = my_strcmp(str1, str2);
    int std_result = strcmp(str1 ? str1 : "", str2 ? str2 : "");  // 标准函数对比
    printf("【%s】\n", scene);
    printf("str1: %s | str2: %s\n", str1 ? str1 : "NULL", str2 ? str2 : "NULL");
    printf("自定义my_strcmp结果: %d | 标准strcmp结果: %d\n", my_result, std_result);
    printf("测试结论: %s\n\n", (my_result > 0 && std_result > 0) || 
                             (my_result < 0 && std_result < 0) || 
                             (my_result == 0 && std_result == 0) ? "通过" : "失败");
}

int main() {
    // 场景1:两个字符串完全相等
    test_my_strcmp("hello", "hello", "场景1:字符串完全相等");
    
    // 场景2:str1更长,前序字符相等
    test_my_strcmp("hello world", "hello", "场景2:str1更长,前序字符相等");
    
    // 场景3:str2更长,前序字符相等
    test_my_strcmp("hello", "hello world", "场景3:str2更长,前序字符相等");
    
    // 场景4:str1对应字符ASCII更大
    test_my_strcmp("helloZ", "helloA", "场景4:str1字符ASCII值更大");
    
    // 场景5:str2对应字符ASCII更大
    test_my_strcmp("helloA", "helloZ", "场景5:str2字符ASCII值更大");
    
    // 场景6:空指针场景
    test_my_strcmp(NULL, NULL, "场景6.1:两个均为空指针");
    test_my_strcmp(NULL, "test", "场景6.2:str1为空指针,str2非空");
    test_my_strcmp("test", NULL, "场景6.3:str1非空,str2为空指针");

    return 0;
}
相关推荐
Shawn_Shawn2 小时前
mcp学习笔记(一)-mcp核心概念梳理
人工智能·llm·mcp
冷雨夜中漫步3 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
33三 三like4 小时前
《基于知识图谱和智能推荐的养老志愿服务系统》开发日志
人工智能·知识图谱
芝士爱知识a4 小时前
【工具推荐】2026公考App横向评测:粉笔、华图与智蛙面试App功能对比
人工智能·软件推荐·ai教育·结构化面试·公考app·智蛙面试app·公考上岸
半桔4 小时前
【IO多路转接】高并发服务器实战:Reactor 框架与 Epoll 机制的封装与设计逻辑
linux·运维·服务器·c++·io
HABuo4 小时前
【linux文件系统】磁盘结构&文件系统详谈
linux·运维·服务器·c语言·c++·ubuntu·centos
腾讯云开发者5 小时前
港科大熊辉|AI时代的职场新坐标——为什么你应该去“数据稀疏“的地方?
人工智能
工程师老罗5 小时前
YoloV1数据集格式转换,VOC XML→YOLOv1张量
xml·人工智能·yolo
我在人间贩卖青春5 小时前
C++之多重继承
c++·多重继承