自定义strcmp函数.

来源:百度知道 编辑:UC知道 时间:2024/09/22 12:35:52
自定义一个strcmp函数.
字符串比较

三楼的兄弟,如果是
"Abc"和"abc"比呢?

*s1=0 是赋值语句吧。

hldzhuzhu 兄弟,你的函数少一个出口.如果是两个字符串相等的话怎么处理呢?

你把函数改了,我记的上次你写的不是这个函数.

#include<iostream>
#include<iomanip>
using namespace std;

int strcmp( char* s1,char* s2)
{
for(;*s1==*s2 && *s1!=0;++s1,++s2);
return *s1-*s2;

}

void test( char* s1,char* s2)
{
int x=strcmp( s1,s2);
cout.width(12);

if( x>0)
cout<<setw(12)<<s1<<" > "<<setw(12)<<s2<<endl;
else if( x==0)
cout<<setw(12)<<s1<<" = "<<setw(12)<<s2<<endl;
else
cout<<setw(12)<<s1<<" < "<<setw(12)<<s2<<endl;
}

int main()
{

test( "traxex","traxex");
test( "hldzhuzhu","zhuzhu");
test( "cpp","c++");

return 0;

}

输出:
$ emacs strcpy