摘要:本文主要向大家介绍了C/C++知识点之C语言实现strcmp,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
本文主要向大家介绍了C/C++知识点之C语言实现strcmp,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
注意转化为unsigned char:strcmp.h
1 #ifndef STRCMP_H
2 #define STRCMP_H
3
4 /***************************************************
5 功能:比较字符串s1和s2。
6 一般形式:strcmp(s1,s2)
7 返回值:
8 当s1<s2时,返回值<0
9 当s1=s2时,返回值=0
10 当s1>s2时,返回值>0
11 ****************************************************/
12
13 int cat_strcmp(const char *src, const char *dst) {
14 int ret = 0;
15
16 while (!(ret = (*(unsigned char *)src - *(unsigned char *)dst)) && *dst)
17 ++src, ++dst;
18
19 if (ret < 0)
20 ret = -1;
21 else if (ret > 0)
22 ret = 1;
23
24 return ret;
25 }
26
27 #endif
main:
1 #include "strcmp.h"
2
3
4 void test_strcmp();
5
6 int main() {
7 test_strcmp();
8
9 return 0;
10 }
11
12 void test_strcmp() {
13 char *s1 = "compare", *s2 = "compase";
14 printf("%d\n", cat_strcmp(s1, s2));
15
16 char *s3 = "compare", *s4 = "compar";
17 printf("%d\n", cat_strcmp(s3, s4));
18
19 char *s5 = "compar", *s6 = "compare";
20 printf("%d\n", cat_strcmp(s5, s6));
21
22 printf("%d\n", cat_strcmp(s3, s6));
23 }
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号