懂C++的进来看看,谢谢!

来源:百度知道 编辑:UC知道 时间:2024/05/16 08:20:13
编写程序,实现两个字符串比较的自定义版:
int strcmp(const char* str1 ,const char * str2);
当str1>str2时,返回正数;
当str1==str2时,返回0;
当str1<str2时,返回负数。
请知道的帮帮忙,非常感谢!!!

给:
int strcmp( const char* str1 ,const char * str2)
{
for(;*str1==*str2 && *str1!=0;++str1,++str2);
return *str1-*str2;

}

下面是验证用的代码:
#include<iostream>
#include<iomanip>
using namespace std;

int strcmp( const char* str1 ,const char * str2)
{
for(;*str1==*str2 && *str1!=0;++str1,++str2);
return *str1-*str2;

}

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&q