不调用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;
}
相关推荐
名字还没想好☜4 小时前
Python f-string 进阶:数字格式化、对齐填充、调试 = 号与嵌套表达式
开发语言·数据库·python·字符串格式化·f-string
IvanCodes4 小时前
RAG 实战教程(一):RAG 工作原理与完整流程——分片、索引、召回、重排和生成
人工智能·后端·agent
·薯条大王4 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
今天AI了吗5 小时前
合规场景下的 AI 推理可解释性:Attention 可视化与推理路径追踪的工程实践
人工智能
周末程序猿5 小时前
浅析大模型推理十二篇之KV Cache
人工智能
鱼樱前端5 小时前
用 AI 做内容变收入
前端·人工智能·ai编程
Dawson Zhu6 小时前
基于Palantir Foundry构建半导体制造AI友好型数据中台:从良率分析场景谈起
人工智能·语言模型·架构·制造·agi
fthux6 小时前
装闭 RenoPit 源码解析(04):装修图纸和合同文件上传处理流程
人工智能·ai·开源·github·open source·renopit
一壶浊酒..7 小时前
Scrape Webpack练习
开发语言·javascript·webpack