头文件:
#ifndef _MYSTRING_H_
#define _MYSTRING_H_
#include <iostream>
namespace dj
{
class mystring
{
private:
char* buff;
public:
mystring();
mystring(const char* str);
mystring(const mystring& str);
virtual ~mystring();
int size() const;
bool empty() const;
const char* c_str() const;
void append(char c);
void append(const char* str);
void append(int i);
void insert(unsigned int pos, char c);
void insert(unsigned int pos, const char* str);
bool remove(char c);
bool remove(const char* str);
bool update(char old_c, char new_c);
int find(char c);
int find(const char* str);
mystring& operator=(const mystring& str);
bool operator==(const mystring& str);
mystring operator+(const mystring& str);
char& operator[](int i);
friend std::ostream& operator<<(std::ostream& out, const mystring& s);
friend std::istream& operator>>(std::istream& in, mystring& s);
};
}
#endif
源文件:
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include "mystring.h"
namespace dj
{
mystring::mystring()
{
this->buff = new char{'\0'};
}
mystring::mystring(const char* str)
{
this->buff = new char[strlen(str) + 1];
strcpy(this->buff, str);
}
mystring::mystring(const mystring& str)
{
this->buff = new char[strlen(str.buff) + 1];
strcpy(this->buff, str.buff);
}
mystring::~mystring()
{
if(this->buff != nullptr)
delete[] this->buff;
}
int mystring::size() const
{
int cnt = 0;
while(this->buff[cnt] != '\0') ++cnt;
return cnt;
}
bool mystring::empty() const
{
return this->buff[0] == '\0';
}
const char* mystring::c_str() const
{
return this->buff;
}
void mystring::append(char c)
{
char* tmp = new char[strlen(this->buff) + 1 + 1];
sprintf(tmp, "%s%c", this->buff, c);
delete[] this->buff;
this->buff = tmp;
}
void mystring::append(const char* str)
{
char* tmp = new char[strlen(this->buff) + strlen(str) + 1];
sprintf(tmp, "%s%s", this->buff, str);
delete[] this->buff;
this->buff = tmp;
}
void mystring::append(int i)
{
char num[15];
sprintf(num, "%d", i);
this->append(num);
}
void mystring::insert(unsigned int pos, char c)
{
if(pos > strlen(this->buff))
pos = strlen(this->buff);
char* tmp = new char[strlen(this->buff) + 1 + 1];
strncpy(tmp, this->buff, pos);
sprintf(tmp + pos, "%c%s", c, this->buff + pos);
delete[] this->buff;
this->buff = tmp;
}
void mystring::insert(unsigned int pos, const char* str)
{
if(pos > strlen(this->buff))
pos = strlen(this->buff);
char* tmp = new char[strlen(this->buff) + strlen(str) + 1];
strncpy(tmp, this->buff, pos);
sprintf(tmp + pos, "%s%s", str, this->buff + pos);
delete[] this->buff;
this->buff = tmp;
}
bool mystring::remove(char c)
{
int pos;
for(pos = 0; this->buff[pos] != '\0' && this->buff[pos] != c; pos++);
if(this->buff[pos] == '\0') return false;
do
{
this->buff[pos] = this->buff[pos + 1];
pos++;
} while (this->buff[pos] != '\0');
return true;
}
bool mystring::remove(const char* str)
{
char* p = strstr(this->buff, str);
if(nullptr == p) return false;
int pos = p - this->buff;
int len = strlen(str);
do
{
this->buff[pos] = this->buff[pos + len];
pos++;
} while (this->buff[pos] != '\0');
return true;
}
bool mystring::update(char old_c, char new_c)
{
int pos;
for(pos = 0; this->buff[pos] != '\0' && this->buff[pos] != old_c; pos++);
if(this->buff[pos] == '\0') return false;
this->buff[pos] = new_c;
return true;
}
int mystring::find(char c)
{
int pos;
for(pos = 0; this->buff[pos] != '\0'; pos++)
{
if(this->buff[pos] == c) return pos;
}
return -1;
}
int mystring::find(const char* str)
{
char* p = strstr(this->buff, str);
if(nullptr == p) return -1;
return p - this->buff;
}
mystring& mystring::operator=(const mystring& str)
{
delete[] this->buff;
this->buff = new char[strlen(str.buff) + 1];
strcpy(this->buff, str.buff);
return *this;
}
bool mystring::operator==(const mystring& str)
{
return !strcmp(this->buff, str.buff);
}
mystring mystring::operator+(const mystring& str)
{
mystring tmp;
tmp.append(this->buff);
tmp.append(str.buff);
return tmp;
}
char& mystring::operator[](int i)
{
return this->buff[i];
}
std::ostream& operator<<(std::ostream& out, const mystring& s)
{
out << s.buff;
return out;
}
std::istream& operator>>(std::istream& in, mystring& s)
{
char tmp[1025];
in.getline(tmp, sizeof(tmp));
delete[] s.buff;
s.buff = new char[strlen(tmp) + 1];
strcpy(s.buff, tmp);
return in;
}
}
调用文件:
#include <iostream>
#include "mystring.h"
#include <string>
using namespace std;
int main()
{
// mystring s1;
// mystring s2("abcd");
// mystring s3 = "123";
// s1 = "hello";
// cout << s1.size() << endl;
// cout << s2.size() << endl;
// cout << s3.size() << endl;
// s3 = "";
// if(s3.empty())
// {
// cout << "s3 is empty!" << endl;
// }
// s1.append(',');
// s1.append("world");
// s1.append(123);
// s1.insert(0, "haha ");
// s1.insert(5, "你好 ");
// s1.remove(' ');
// s1.remove("123");
// if(!s1.remove("432"))
// {
// cout << "删除失败!" << endl;
// }
// cout << s1 << endl;
// while(s1.update('a', 'b'));
// cout << s1 << endl;
// mystring userName;
// cout << "用户名:";
// cin >> userName;
// if(userName == "zhangsan")
// {
// cout << "用户名正确!" << endl;
// }
// printf("%s\n", userName.c_str());
// s1 = s2 + s3 + "abc";
// s1 = mystring("abc") + "def";
// cout << s1 << endl;
// string s = "abcdef";
// const char* p = s.c_str();
// for(int i = 0; i < s.size(); i++)
// cout << s[i] << endl;
// s[2] = 'm';
// cout << s << endl;
using namespace dj;
mystring s = "fdsafdlfldasfkljdsafkljdsafkljdsafkljdsafd;asfdasf";
cout << sizeof(s) << endl;
cout << s.size() << endl;
return 0;
}