Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class MyString {
- char * strPtr;
- unsigned int strSize, strCapacity;
- public:
- MyString() {
- clear();
- }
- MyString(const char * string) {
- strCapacity = strlen(string);
- cout << "String size is " << strCapacity << endl;
- strSize = strCapacity;
- strPtr = new char(strCapacity + 1);
- strcpy(strPtr, string);
- }
- MyString(MyString&a){
- strCapacity = a.strCapacity;
- strSize = strCapacity;
- strPtr = new char(a.strCapacity + 1);
- strcpy(strPtr, a.strPtr);
- }
- ~MyString(){
- delete[] strPtr;
- }
- void reserve (unsigned int c){
- if (strCapacity < c){
- char * cpy = new char(strCapacity + 1);
- strcpy(cpy, strPtr);
- strCapacity = c;
- strPtr = new char(strCapacity + 1);
- strcpy(strPtr, cpy);}
- }
- MyString & append(MyString & str) {
- strSize = strSize + str.strSize;
- reserve(str.strCapacity + strCapacity + 1);
- strcat(strPtr, str.strPtr);
- }
- MyString & assign(MyString & str) {
- reserve(str.strCapacity);
- strcpy(strPtr, str.strPtr);
- }
- const char * c_str() {
- return strPtr;
- }
- unsigned int size(){
- return strSize;
- }
- unsigned int capacity(){
- return strCapacity;
- }
- void clear(){
- strPtr = new char(1);
- strPtr[0] = '\0';
- }
- bool empty(){
- if (strPtr[0] == '\0')
- return true;
- else
- return false;
- }
- };
- int main(){
- MyString s1;
- MyString s2("I hate my fucking life please work");
- cout << s1.c_str() << endl;
- cout << s2.c_str() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement