zoukankan      html  css  js  c++  java
  • c++之手写strcmp

    int strcmp(const char* str1, const char*str2){
    assert(str1 != NULL&&str2 != NULL);
    while (*str1&&*str1 == *str2){
    str1++;
    str2++;
    }

    if (*(unsigned char*)str1 < *(unsigned char*)str2){
    return -1;
    }
    else if (*(unsigned char*)str1 > *(unsigned char*)str2){
    return 1;
    }
    else{
    return 0;
    }

    }

    注意:
    1.参数是 const
    2.异常输入处理 assert(str1 != NULL&&str2 != NULL);
    3.字符之间大小比较时一定要先将char*型指针先转换为unsigned char*
    因为有符号字符值的范围是-128~127,无符号字符值的范围是0~255,而字符串的ASCII没有负值。
    例如 *str1的值为1,*str2的值为255。
    本来*str1<*str2,但是有符号数中255是-1.

  • 相关阅读:
    MySQL大数据分页调优实践
    CentOS 搭建L2TP
    CentOS 搭建SS5
    CentOS 搭建PPTP
    CentOS 防火墙 firewalld
    CentOS 初始化账号
    nginx升级与回退
    Python
    python
    linux
  • 原文地址:https://www.cnblogs.com/Pond-ZZC/p/11820812.html
Copyright © 2011-2022 走看看