C++定义一个string类,如何通过其成员函数stringleft,stringright实现字符串左右两侧空白字符的过滤

2025-05-18 07:56:42
推荐回答(2个)
回答1:

class MyString
{
public:
MyString();//构造不写了。使用new或者malloc分配空间构造字符串,记得析构的时候释放,不写了啊
Left();
Right();
private:
char * str;
};
MyString::Left()
{
char *p = str;
while(*p = " " && p != NULL)
{
strcpy(p,p+1);
p= str;
}
}
MyString::Right()
{
char *p = str+strlen(str);
while(p != NULL && *p == " ")
{
*p = "\0";
p--;
}
}

回答2:

把左右两侧的“ ”替换成“”就行吧。。。