不调用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;
}
相关推荐
阿白逆袭记2 小时前
Git原理与使用详解(七):团队交响曲——多人协作开发实战
git
茉莉玫瑰花茶2 小时前
脚手架介绍
c++
幸福的达哥2 小时前
Python多线程、多进程、协程、锁、同步、异步的详解和应用
开发语言·python
码农三叔2 小时前
(6-1)手部、足部与末端执行器设计:仿生手设计
人工智能·架构·机器人·人形机器人
liliangcsdn2 小时前
RL中GAE的计算过程详解
大数据·人工智能·算法
Hgfdsaqwr2 小时前
内存泄漏检测与防范
开发语言·c++·算法
yhyvc2 小时前
人形具身机器人国产/进口快速选型优先级清单
人工智能·机器人
熬夜敲代码的小N2 小时前
Python基础入门:环境配置全指南+核心语法解析
开发语言·python
wangmengxxw2 小时前
SpringAI-mysql
java·数据库·人工智能·mysql·springai