代码实现
/*********************************************************
* > File Name: my_strcmp.c
* > Author: YQQ
* > Mail:
* > Created Time: 2021年04月23日 星期五 22时02分30秒
**************************************************************/
#include<stdio.h>
#include<strings.h>
#include<stdlib.h>
#include<string.h>
#define N 20
int my_strcmp(char *pa, char *pb, int len);
void print(int len1, int len2, char *a, char *b);
int main(int argc, char * argv[])
{
char a[N];
char b[N];
int len1, len2;
printf("please string a:\n");
gets(a);
printf("please string b:\n");
gets(b);
len1 = strlen(a);
len2 = strlen(b);
print(len1, len2, a, b);
return 0;
}
int my_strcmp(char *pa, char *pb, int len)
{
for(int i = 0; i < len; i++)
{
if(*(pa+i) < *(pb+i))
return -1;
else if(*(pa+i) > *(pb+i))
return 1;
else
continue;
}
return 0;
}
void print(int len1, int len2, char *a, char *b)
{
int ret;
if(len1 == len2)
{
ret = my_strcmp(a, b, len1);
if(ret == 0)
printf("字符串a == 字符串b\n");
if(ret == 1)
printf("字符串a > 字符串b\n");
if(ret == -1)
printf("字符串a < 字符串b\n");
}
else if(len1 > len2)
{
ret = my_strcmp(a, b, len2);
if(ret == 0 || ret == 1)
printf("字符串a > 字符串b\n");
else if(ret == -1)
printf("字符串a < 字符串b\n ");
}
else
{
ret = my_strcmp(a, b, len1);
if(ret == 0 || ret == -1)
printf("字符串a < 字符串b\n");
if(ret == 1)
printf("字符串a > 字符串b\n");
}
}
结果展示: