首页 > 代码库 > 自己实现的strcmp函数
自己实现的strcmp函数
#include <iostream>
using namespace std;
// 自己实现strcmp函数
int Strcmp(char to[], char from[]);
int main()
{
char to[30], from[30];
int flag;
cout << "请输入第一个字符串:";
cin >> to;
cout << "请输入第二个字符串:";
cin >> from;
flag = Strcmp(to, from);
if (1 == flag)
cout << to << "大于" << from << endl;
else if (0 == flag)
cout << to << "等于" << from << endl;
else
cout << to << "小于" << from << endl;
system("pause");
return 0;
}
int Strcmp(char to[], char from[])
{
int i = 0;
for (; ‘\0‘ != to[i] && ‘\0‘ != from[i]; i++)
if (to[i] > from[i])
return 1;
else if (to[i] < from[i])
return -1;
if (‘\0‘ == to[i])
return -1;
else if (‘\0‘ == from[i])
return 1;
else
return 0;
}
using namespace std;
// 自己实现strcmp函数
int Strcmp(char to[], char from[]);
int main()
{
char to[30], from[30];
int flag;
cout << "请输入第一个字符串:";
cin >> to;
cout << "请输入第二个字符串:";
cin >> from;
flag = Strcmp(to, from);
if (1 == flag)
cout << to << "大于" << from << endl;
else if (0 == flag)
cout << to << "等于" << from << endl;
else
cout << to << "小于" << from << endl;
system("pause");
return 0;
}
int Strcmp(char to[], char from[])
{
int i = 0;
for (; ‘\0‘ != to[i] && ‘\0‘ != from[i]; i++)
if (to[i] > from[i])
return 1;
else if (to[i] < from[i])
return -1;
if (‘\0‘ == to[i])
return -1;
else if (‘\0‘ == from[i])
return 1;
else
return 0;
}
自己实现的strcmp函数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。