【问题描述】实现Mystring类
类结构如下
class Mystring
{
private:
char *str;//字符串指针
unsigned len;//字符串长度
public:
Mystring(const char *val = “”);//构造函数
~Mystring();//析构函数,打印"destructed"+字符串与字符串长度,空格隔开,具体样式参考测试样例
Mystring(Mystring &cstr);//拷贝构造函数
//字符串切割,返回一个新的Mystring,sta为切割起始位置,length为切割长度
Mystring substr(unsigned sta = 0,unsigned length = 1);
unsigned length();//返回字符串长度
char *getStr();//获取字符串
/*实现按指定位置插入指定数量的指定字符,p:待插入位置起始下标,n:待插入字符数量,c:待插入字符
*插入成功时返回true,失败返回false
* */
bool insert(unsigned p = 0,unsigned n = 1,char c = ’ ');
/*实现删除从指定位置开始的指定数量的字符,p:待删除位置起始下标,n:待删除字符数量
*删除成功时返回true,删除失败返回false
*/
bool erase(unsigned p = 0,unsigned n = 1);
};
主函数结构如下:
int main()
{
char s[100];
cin>>s;
Mystring mstr(s);
Mystring submstr(mstr.substr(0,11));
cout<< "submstr: "<<submstr.getStr()<<endl;
submstr.erase(0,5);
cout<<submstr.getStr()<<endl;
submstr.insert(0,5,‘c’);
cout<<"after insert: "<<submstr.getStr()<<endl;
return 0;
}
#请勿修改主函数,请勿使用C++的string类#
【输入形式】字符串
【输出形式】调用所给main函数
【样例输入】
hello,world
【样例输出】
destructed hello,world 11
submstr: hello,world
after erase: ,world
after insert: ccccc,world
destructed ccccc,world 11
destructed hello,world 11
#include<iostream>
#include<string.h>
using namespace std;
class Mystring
{
private:
char* str;//字符串指针
unsigned len;//字符串长度
public:
Mystring(const char* val = "");//构造函数
~Mystring();//析构函数,打印"destructed"+字符串与字符串长度,空格隔开,具体样式参考测试样例
Mystring(const Mystring& cstr);//拷贝构造函数
//字符串切割,返回一个新的Mystring,sta为切割起始位置,length为切割长度
Mystring substr(unsigned sta = 0, unsigned length = 1);
unsigned length();//返回字符串长度
char* getStr();//获取字符串
/*实现按指定位置插入指定数量的指定字符,p:待插入位置起始下标,n:待插入字符数量,c:待插入字符
*插入成功时返回true,失败返回false
* */
bool insert(unsigned p = 0, unsigned n = 1, char c = ' ');
/*实现删除从指定位置开始的指定数量的字符,p:待删除位置起始下标,n:待删除字符数量
*删除成功时返回true,删除失败返回false
*/
bool erase(unsigned p = 0, unsigned n = 1);
};
Mystring::Mystring(const char* val) {
len = strlen(val);
str = new char[100];
strcpy(str , val);
}
Mystring::~Mystring()
{
cout << "destructed " << str << " " << len<<endl;
delete[] str;
}
/* 拷贝构造函数 */
Mystring::Mystring(const Mystring& cstr)
{
str = new char[100];
strcpy(str, cstr.str);
len = cstr.len;
}
Mystring Mystring::substr(unsigned sta, unsigned length) {
char a[100];
int l = 0;
memset(a, NULL, sizeof(a));//初始
for (int i = sta; i < sta + length; i++) {
a[l++] = str[i];
}
Mystring newStr(a);
Mystring sb(a);
//cout << "destructed " << a << " " << strlen(a) << endl;
return newStr;
}
char* Mystring::getStr() {
return str;
}
bool Mystring::erase(unsigned int p, unsigned int n) {
char a[100];
memset(a, NULL, sizeof(a));//初始
int i;
for (i = 0; i < p; i++) {
a[i] = str[i];
}
int l = i;
for (i; i < l + n + 1; i++)
{
a[i] = str[i + n];
}
strcpy(str, a);
len -= n;
return true;
};
bool Mystring::insert(unsigned int p, unsigned int n, char c) {
char a[100];
memset(a, NULL, sizeof(a));//初始
int i;
for (i = 0; i < p; i++) {
a[i] = str[i];
}
int l = i;
for (i; i < l + n; i++) {
a[i] = c;
}
for (i; i < len + n; i++) {
a[i] = str[i - n];
}
strcpy(str, a);
len += n;
return true;
}
int main()
{
char s[100];
cin >> s;
Mystring mstr(s);
Mystring submstr(mstr.substr(0,11));
cout << "submstr: " << submstr.getStr() << endl;
submstr.erase(0, 5);
cout <<"after erase: " << submstr.getStr() << endl;
submstr.insert(0, 5, 'c');
cout << "after insert: " << submstr.getStr() << endl;
return 0;
}