利用指针实现strncmp函数功能
作者:互联网
利用指针实现strncmp函数功能
#include<stdio.h>
#include<string.h>
int mstrncmp( char *s1, char *s2, size_t n)
{
while(*s1 && *s2 && (*s1 = *s2) && (n--))
{
s1++;
s2++;
}
#if 1
if((*s1 - *s2) > 0)
return 1;
else if((*s1 - *s2) < 0)
return -1;
else
return 0;
#else
return *s1 - *s2;
#endif
}
int main(int argc, const char *argv[])
{
char str1[] = {"sjkab"};
char str2[] = {"ajbhfjkabh"};
printf("mstrcmp: %d\n", mstrncmp(str1, str2, 0));
printf("strcmp: %d\n", strncmp(str1, str2, 0));
return 0;
}```
标签:strncmp,return,函数,int,s2,s1,char,指针 来源: https://blog.csdn.net/Mr__Shen/article/details/111970663