#include<iostream>
#include<string>
using namespace std;
class CMystring
{
public:
CMystring(char *);
int length(); //求长度
int compare(CMystring &); //两个字符串比较
private:
char * str;
}
CMystring::CMystring(char*p)
{
str=p;
}
int CMystring::length()
{
int len=0;
while(*str!='\0')
{
str++;
len++;
}
return len;
}
int CMystring::compare(CMystring & pp)
{
if(this->length()>pp.length())
{
return 1;
}
else if(this->length()<pp.length())
{
return -1;
}
else
{
return 0;
}
}
int main()
{
CMystring c1("good");
CMystring c2("better");
cout<<c1.length()<<endl; //输出c1的长度
cout<<c2.length()<<endl; //输出c2 的长度
cout<<c1.compare(c2)<<endl;
return 0;
}
在测试字符串的比较时错了,却找不出原因,求帮忙,谢谢 |