不调用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;
}
相关推荐
caimouse2 小时前
reactos编码规范
c语言·开发语言
小雨下雨的雨3 小时前
井字棋AI机器人实现详解 - Minimax算法实战-鸿蒙PC Electron框架完成
前端·人工智能·算法·华为·electron·鸿蒙
xieliyu.6 小时前
Java算法精讲:双指针(三)
java·开发语言·算法
我没胡说八道6 小时前
高校论文AI检测优化工具对比研究与实测分析(2026)
人工智能·深度学习·机器学习·计算机视觉·aigc·论文
秦亚伟6 小时前
AI浪潮重塑融资租赁行业新格局
人工智能
love530love6 小时前
LiveTalking 数字人项目 Windows 部署完全指南(EPGF 架构)
人工智能·windows·python·架构·livetalking·epgf
元启数宇6 小时前
喷淋AI布点实战:8小时人工布点→20分钟自动出图
人工智能
哈哈,柳暗花明6 小时前
人工智能专业术语详解(H)
人工智能·专业术语
圣殿骑士-Khtangc6 小时前
AI 编程工具 2026 实战横评:Cursor 3 vs Claude Code vs Copilot,开发者选型完全指南
人工智能·copilot
云器科技6 小时前
云器Lakehouse 2026年5月版本发布:拥抱 AI Agent,重塑数据智能开发新范式
人工智能