首页 > 编程语言 > 详细

《从零开始的C语言生活》实现简易的strcmp函数功能

时间:2021-05-18 09:08:09      阅读:17      评论:0      收藏:0      [点我收藏+]
#include <stdio.h>
int MyStrcmp(char *p1,char *p2);
int MyStrcmp(char *p1,char *p2){

    for(;*p1==*p2;p1++,p2++){
        if(*p1==‘\0‘){
            return 0;
        }

    }
    return *p1-*p2;
}

int main()
{
    char a[]="Hello";
    char b[]="Helly";
    printf("diffrence %d\n",MyStrcmp(a,b));
char c[]="word";
char d[]="word";
printf("same result is: %d",MyStrcmp(c,d));

}

或是使用:

#include <stdio.h>
int MyStrcmp(char s[],char t[]){
int i;
for(i=0;s[i]==t[i];i++){
    if(s[i]==‘\0‘){
        return 0;
    }

}
return (s[i]-t[i]);
}
int main()
{
    char a[]="Hello";
    char b[]="Helly";
    printf("diffrence %d\n",MyStrcmp(a,b));
char c[]="word";
char d[]="word";
printf("same result is: %d",MyStrcmp(c,d));

}

《从零开始的C语言生活》实现简易的strcmp函数功能

原文:https://www.cnblogs.com/tysec/p/14779030.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!