#include<iostream>
#include<cstring>
using namespace std;
int str_len(char * a)
{
int i;
for(i=0;a[i]!='\0';i++);
return i;
}
int str_cmp(char * a,char * b)
{
int a_size = str_len(a);
int b_size = str_len(b);
if(a_size>b_size)
return 1;
else if(a_size<b_size)
return -1;
else
{
for(int i=0;i<a_size;i++)
{
if(a[i]>b[i])
return 1;
else if(a[i]<b[i])
return -1;
}
}
return 0;
}
int main()
{
char a_1[10] = "pause";
char a_2[10] = "Pause";
char b_1[10] = "hello";
char b_2[10] = "ahh";
cout<<"str_cmp:"<<str_cmp(a_1,a_2)<<"\t";
cout<<"strcmp:"<<strcmp(a_1,a_2)<<endl;
cout<<"str_cmp:"<<str_cmp(b_1,b_2)<<"\t";
cout<<"strcmp:"<<strcmp(b_1,b_2)<<endl;
cout<<"str_cmp:"<<str_cmp(a_1,b_2)<<"\t";
cout<<"strcmp:"<<strcmp(a_1,b_2)<<endl;
cout<<"str_cmp:"<<str_cmp(a_2,b_1)<<"\t";
cout<<"strcmp:"<<strcmp(a_2,b_1)<<endl;
system("pause");
return 0;
}
C++编写一函数,比较两个字符串的大小,不能用strcmp
最新推荐文章于 2024-06-23 11:08:26 发布