用c++实现字符串操作类,不使用字符串操作的系统函数

成员函数需要有:str_copy,str_compare,str_lenth,字符串可以用‘+’实现两个字符串连接,所有的字符串都用char*或者char数组。

#include <iostream>
#include <cstring>

class String {
private:
    char* str;

public:
    // 构造函数
    String() {
        str = nullptr;
    }

    String(const char* s) {
        int len = strlen(s);
        str = new char[len + 1];
        strcpy(str, s);
    }

    String(const String& s) {
        int len = strlen(s.str);
        str = new char[len + 1];
        strcpy(str, s.str);
    }

    // 析构函数
    ~String() {
        if (str) {
            delete[] str;
        }
    }

    // 重载赋值运算符
    String& operator=(const String& s) {
        if (this != &s) {
            if (str) {
                delete[] str;
            }
            int len = strlen(s.str);
            str = new char[len + 1];
            strcpy(str, s.str);
        }
        return *this;
    }

    // 重载 "+" 运算符
    String operator+(const String& s) {
        String res;
        int len1 = str ? strlen(str) : 0;
        int len2 = s.str ? strlen(s.str) : 0;
        int len = len1 + len2;
        res.str = new char[len + 1];
        if (str) {
            strcpy(res.str, str);
        }
        if (s.str) {
            strcpy(res.str + len1, s.str);
        }
        return res;
    }

    // 重载 "==" 运算符
    bool operator==(const String& s) {
        return strcmp(str, s.str) == 0;
    }

    // 重载 "!=" 运算符
    bool operator!=(const String& s) {
        return strcmp(str, s.str) != 0;
    }

    // 重载 "<" 运算符
    bool operator<(const String& s) {
        return strcmp(str, s.str) < 0;
    }

    // 重载 ">" 运算符
    bool operator>(const String& s) {
        return strcmp(str, s.str) > 0;
    }

    // 返回字符串长度
    int str_length() const {
        return strlen(str);
    }

    // 复制字符串
    void str_copy(const char* s) {
        if (str) {
            delete[] str;
        }
        int len = strlen(s);
        str = new char[len + 1];
        strcpy(str, s);
    }

    // 比较字符串
    int str_compare(const char* s) const {
        return strcmp(str, s);
    }

    // 输出字符串
    void print() const {
        std::cout << str << std::endl;
    }
};

int main() {
    String s1("hello");
    String s2("world");
    String s3("hello");

    std::cout << "s1 = ";
    s1.print();
    std::cout << "s2 = ";
    s2.print();
    std::cout << "s3 = ";
    s3.print();

    std::cout << "s1 == s2 ? " << (s1 == s2 ? "true" : "false") << std::endl;
    std::cout << "s1 == s3 ? " << (s1 == s3 ? "true" : "false") << std::endl;
    std::cout << "s1 != s2 ? " << (s1 != s2 ? "true" : "false") << std::endl;
    std::cout << "s1 < s2 ? " << (s1 < s2 ? "true" : "false") << std::endl;
    std::cout << "s1 > s2 ? " << (s1 > s2 ? "true" : "false") << std::endl;

    String s4 = s1 + s2;
    std::cout << "s1 + s2 = ";
    s4.print();

    s1.str_copy("world");
    std::cout << "after s1.str_copy(\"world\") s1 = ";
    s1.print();
    std::cout << "s1.str_compare(\"world\") = " << s1.str_compare("world") << std::endl;
    std::cout << "s1.str_length() = " << s1.str_length() << std::endl;

    return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值