DEV Community

Quoc Bao
Quoc Bao

Posted on • Edited on

String class implementation

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.

class STRING
{
private:
    char *content;
    int len;

    void deepcopy(const STRING &other);

public:
    STRING();
    // ~STRING();
    STRING(std::string);

    STRING(const STRING &other);
    STRING &operator=(const STRING &other);

    bool operator==(STRING &other);
    bool operator==(std::string);
    bool operator!=(STRING other);

    char getContent(int);
    char *getWholeContent();
    void deleteContent();
    void allocateContent(int);
    int getLen();
    void setLen(int);
    void setContent(int, char);

    void input();
    void output();

    void removeBlankHead();
    void removeBlankTail();
    void RemoveExtraSpaces();

    friend std::istream &operator>>(std::istream &in, STRING &a);
    friend std::ostream &operator<<(std::ostream &out, STRING a);
};
Enter fullscreen mode Exit fullscreen mode

Read more here!

Top comments (0)