//编写函数mycmp实现库函数strcmp的功能 两个字符串的比较
#include <stdio.h>
#include <string.h>
int mycmp(char *p1, char *p2);
int main() {
char str1[10], str2[10];
printf("分别输入字符串1和字符串2:\n");
scanf("%s", str1);
scanf("%s", str2);
char *p1 = str1, * p2 = str2;
int k;
k = mycmp(p1, p2);
if (k < 0)
printf("字符串1<字符串2");
else if (k > 0)
printf("字符串1>字符串2");
else
printf("字符串1=字符串2");
return 0;
}
int mycmp(char *p1, char *p2) {
int i;
for (i = 0; p1[i] != '\0' && p2[i] != '\0'; i++)
if (p1[i] != p2[i])
return p1[i] - p2[i];
return p1[i] = p2[i];
}
运行结果