c++自己定义比较字符串大小的函数

#include “stdafx.h”
#include
using namespace std;
int my_strcmp(char s1[], char s2[]);
int main()
{
char s1[] = “hello world”;
char s2[] = “hello abc”;
int flag = my_strcmp(s1, s2);
if (flag == 0)
{
//printf("%s 等于 %s\n", s1, s2);
cout << s1 << “等于” << s2 << endl;
}
else if (flag > 0)
{
//printf("%s 大于 %s\n", s1, s2);
cout << s1 << “大于” << s2 << endl;
}
else
{
//printf("%s 小于 %s\n", s1, s2);
cout << s1 << “小于” << s2 << endl;
}

return 0;

}
int my_strcmp(char s1[], char s2[])
{
int i = 0;
while (s1[i] && s2[i] != ‘0’&&s1[i]==s2[i])
{
//if (s1[i] == s2[i])
//{
i++;
//}
/* else if (s1[i] > s2[i])
{
return 1;
}
else if (s1[i] < s2[i])
{
return -1;
}
else
return 0;*/
}
return s1[i]-s2[i];
}

猜你喜欢

转载自blog.csdn.net/weixin_43210805/article/details/82989075