代码如下,如有帮助,请采纳一下,谢谢。
#include<stdio.h>
#include <process.h>
int strcmp(char* p1,char* p2)
{
while( (*p1 != 0) && (*p2 != 0) )
{
if (*p1 > *p2)
{
return 1;
}else if (*p1 < *p2)
{
return -1;
}
p1++;
p2++;
}
if ( (*p1 == 0) && (*p2 == 0))
{
return 0;
}else if ((*p1 == 0) && (*p2 != 0))
{
return -1;
}else
return 1;
}
int main()
{
char buf1[100] = {0};
char buf2[100] = {0};
gets(buf1);
gets(buf2);
printf("%d\n",strcmp(buf1,buf2));
system("pause");
return 0;
}
思路:
1.循环读取下标相同的2个字符串的字符;
2.ascii值大的字符串,返回1,即字符串1大于字符串2;
3.ascii值相同就再比较后面的,如果都相同返回0,即相等;
4.ascii值小的返回-1,即小于;
5.如果字符串都相同,长的字符串大于短的字符串。