#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int mystrcmp(const char *s1, const char *s2)
{
int i=0;
while ((s1[i] != '\0')&&(s2[i] != '\0')&&(s1[i] == s2[i]))
{
i++;
}
if (s1[i] == s2[i])
return 0;
else if (s1[i] > s2[i])
return 1;
else
return -1;
}
int mystrncmp(const char *s1, const char *s2, int n)
{
int i = 0;
while ((s1[i] != '\0') && (s2[i] != '\0') && (s1[i] == s2[i]) && (i < n-1))
{
i++;
}
if (s1[i] == s2[i])
return 0;
else if (s1[i] > s2[i])
return 1;
else
return -1;
}
int main(void)
{
char *p1 = "bcd";
char *p2 = "cc";
printf("%d", mystrcmp(p1, p2));
printf("%d", mystrncmp(p1, p2,2));
return 0;
}
字符串(二)strcmp、strncmp的实现
最新推荐文章于 2024-09-07 00:02:23 发布