C++ 实现strcmp
来源:互联网 发布:东方国信java工资 编辑:程序博客网 时间:2024/05/20 21:21
实现strcmp函数,不使用任何的字符串库
#include <iostream>#include <cassert>#include <cstring>using namespace std;int _strcmp(const char* s1, const char* s2) { assert(s1 != NULL && s2 != NULL); while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2) { s1++; s2++; } if (*s1 > *s2) { return 1; } else if (*s1 < *s2) { return -1; } else { return 0; }}int main() { char* s1 = "123"; char* s2 = "1234"; char* s3 = "123"; char* s4 = "234"; cout << _strcmp(s1, s2) << endl; cout << _strcmp(s2, s1) << endl; cout << _strcmp(s3, s1) << endl; cout << _strcmp(s4, s2) << endl; cout << endl; cout << strcmp(s1, s2) << endl; cout << strcmp(s2, s1) << endl; cout << strcmp(s3, s1) << endl; cout << strcmp(s4, s2) << endl;}
输出结果:
-1
1
0
1
-1
1
0
1
0 0
- c语言strcmp实现
- C语言实现strcmp
- C语言实现strcmp
- C语言实现strcmp
- 【C语言】 实现strcmp
- C/C++--strcmp函数实现
- C函数库中的strcmp实现
- C语言实现strcmp函数
- 用c语言实现strcmp
- C/C++——strcmp函数实现
- C/C++——strcmp函数实现
- strcmp 字符串比较,c语言实现
- c语言实现strcmp和strcpy
- 用C实现strcpy、strcmp、strlen函数
- C 实现strcmp,strcpy,strcat函数
- C语言实现strcmp()和strcpy()函数
- 【c语言】不用库函数实现strcmp
- 【C语言】strcmp函数的实现
- 重载课堂作业
- iOS 获取UIWebView上面的按钮点击事件
- 【转】C++构造函数详解及显式调用构造函数
- 仿射空间
- STM32|4-20mA输出电路
- C++ 实现strcmp
- 转的
- Shell脚本全面学习
- 记录一下,dojo tabContainer 引入 iframe的方法
- 开发APP没有数据怎么办?
- android sdk 设置
- Form的高度有限制,form的高度只能比屏幕的高度大一点点
- cocos2d-x 3.5版 创建新工程
- android R文件无法自动生成