不调用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;
}
相关推荐
2401_8920709811 小时前
【Linux C++ 日志系统实战】LogFile 日志文件管理核心:滚动策略、线程安全与方法全解析
linux·c++·日志系统·日志滚动
美酒没故事°12 小时前
Open WebUI安装指南。搭建自己的自托管 AI 平台
人工智能·windows·ai
yuzhuanhei12 小时前
Visual Studio 配置C++opencv
c++·学习·visual studio
云烟成雨TD12 小时前
Spring AI Alibaba 1.x 系列【6】ReactAgent 同步执行 & 流式执行
java·人工智能·spring
Wenweno0o12 小时前
0基础Go语言Eino框架智能体实战-chatModel
开发语言·后端·golang
AI攻城狮12 小时前
用 Obsidian CLI + LLM 构建本地 RAG:让你的笔记真正「活」起来
人工智能·云原生·aigc
鸿乃江边鸟12 小时前
Nanobot 从onboard启动命令来看个人助理Agent的实现
人工智能·ai
lpfasd12312 小时前
基于Cloudflare生态的应用部署与开发全解
人工智能·agent·cloudflare
俞凡12 小时前
DevOps 2.0:智能体如何接管故障修复和基础设施维护
人工智能
comedate12 小时前
[OpenClaw] GLM 5 关于电影 - 人工智能 - 的思考
人工智能·电影评价