String class implementation

Image source: https://zhuanlan.zhihu.com/p/355342722

Hello everybody! Today, I would like to show you a quick implementation of a string class.

A string is an array of characters. So, we initialize it with an array of char type and a length.

char *content;
int len;

Next, we initialize a NULL string with a length of zero in the default constructor and an input of an std::string in the parameterized constructor.

STRING::STRING()
{
    this->content = NULL;
    this->len = 0;
}

STRING::STRING(std::string str)
{
    _allocate(this->content, str.size() + 1);
    for (int i = 0; i < str.size(); i++)
    {
        this->content[i] = str[i];
    }
    this->len = str.size();
}

Next, we come to the copy constructor and copy assignment. The copy constructor initializes an object by another object. Rather than pass by reference (two objects pointing to the same array), the copy constructor and copy assignment creates a newly distinct object with the help of the deepcopy function which prevents the alteration of one object affecting another. The copy assignment is, in fact, an equal sign overloaded. Just like when assigning one variable to another, and now one string is assigned to another.

STRING::STRING(const STRING &other)
{
    if (this->content != NULL)
        delete[] this->content;
    _allocate(this->content, other.len + 1);
    this->len = other.len;
    this->deepcopy(other);
}

STRING &STRING::operator=(const STRING &other)
{
    if (this == &other)
        return *this;
    if (this->content != NULL)
        delete[] this->content;
    _allocate(this->content, other.len + 1);
    this->len = other.len;
    this->deepcopy(other);
    return *this;
}

In case a string instance already has some content, make sure you delete it before copy.

void STRING::input()
{
    std::string str;
    getline(std::cin, str);
    if (this->content != NULL)
        delete[] this->content;
    _allocate(this->content, str.size() + 1);
    for (int i = 0; i < str.size(); i++)
    {
        this->content[i] = str[i];
    }
    this->len = str.size();
}

void STRING::output()
{
    std::string str;
    for (int i = 0; i < this->len; i++)
    {
        str.push_back(this->content[i]);
    }
    cout << str;
}

The *input method borrows an std::string, copies the content of the input string to itself using the push_back method letter by letter. Similarly, the output method borrows an std::string, copies the content of itself to the std::string, and prints out the output.

*Note: the set of length equal to str.size() shouldn’t be forgotten, or else other methods won’t work.

std::istream &operator>>(std::istream &in, STRING &a)
{
    std::string str;
    getline(in, str);
    if (a.getWholeContent() != NULL)
        a.deleteContent();
    a.allocateContent(str.size() + 1);
    for (int i = 0; i < str.size(); i++)
    {
        a.setContent(i, str[i]);
    }
    a.setLen(str.size());
    return in;
}

std::ostream &operator<<(std::ostream &out, STRING a)
{
    std::string str;
    for (int i = 0; i < a.getLen(); i++)
    {
        str.push_back(a.getContent(i));
    }
    out << str;
    return out;
}

The cin and cout operator takes in and outputs a string just like the input and output methods work.

void STRING::RemoveExtraSpaces()
{
    this->removeBlankHead();
    this->removeBlankTail();
    for (int i = 0; i < this->len; i++)
    {
        if (this->content[i] == ' ' && this->content[i + 1] == ' ')
        {
            for (int j = i; j < this->len; j++)
            {
                this->content[j] = this->content[j + 1];
            }
            this->len--;
            i--; // update i!
        }
    }
}

There is an extra method provided which removes extra spaces in a string. This includes no blank spaces at the beginning and the end. Only one space is allowed in between words.

Bao Ca Ca

HCM 27.04.22 11:34 a.m.

If you want the code, please refer to them as follows.

String.h

string_class.cpp

string_main.cpp

2 bình luận cho “String class implementation”

Bình luận về bài viết này

Tạo trang giống vầy với WordPress.com
Tham gia