实现一个字符串类,可以比较和连接两个字符串

¥¥这是我写的第一个实际意义上的c++程序,很简单,但是第一次写的时候半夜坐在电脑前急哭了…不过好在这个程序让我收获了很多,也终于有点明白了c和c++的区别、明白了this的用法。也希望这个小程序能帮到一些初学c++的孩子¥¥

#include <stdio.h>
#include <string.h>

using namespace std;

class String{
private:
    char *s;
    int len;
public:
    String(){
        this->s = new char[10000];         //this均可省去
        this->len = 0;
    }
    String(char *ss){
        this->len = strlen(ss);
        this->s = new char(strlen(ss) + 1);
        for(int i = 0; i < this->len; i++)
            s[i] = ss[i];
        s[strlen(ss)] = '\0';
    }
    String(String &ss){
        s = new char(ss.len + 1);
        this->len = ss.len;
        for(int i = 0; i < this->len; i++)
            s[i] = ss.s[i];
        s[ss.len + 1] = '\0';
    }
    ~String(){
        delete []s;
        s = NULL;
    }
    void cmp(char *ss) const{
        int length = strlen(ss);
        int tab = 1;
        if(this->len == length){
            for(int i = 0; i < length; i++){
                if(ss[i] == s[i])
                    continue;
                else{
                    tab = 0;
                    printf("两字符串不相等\n");
                    break;
                }
            }
            if(tab)
                printf("两字符串相等\n");
        }
        else printf("两字符串不相等\n");
    }
    void cnt(String &ss){
        for(int i = 0; i < this->len; i++){
            ss.s[ss.len] = s[i];
            ss.len++;
        }
    }
    char *gets(){
        return this->s;
    }
    int getlen(){
        return this->len;
    }
};
int main()
{
    char a[100], b[100];
    scanf("%s", a);
    scanf("%s", b);
    String s1(a);
    s1.cmp(b);

    String s2(b);
    String s3;
    s1.cnt(s3);
    s2.cnt(s3);
    char *p;
    p = s3.gets();
    printf("连接好的字符串为:");
    for(int i = 0; i < s3.getlen(); i++){
        printf("%c ", *p);
        p++;
    }
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己实现字符串 class CMStringImp; class CMstring { public: explicit CMstring(void); ~CMstring(void); CMstring(LPCTSTR lpszstr); CMstring(const CMstring& lpszstr); CMstring& operator = (const CMstring& lpszstr); operator LPCTSTR() const; bool operator == (const CMstring&) const; bool operator != (const CMstring&) const; bool operator < (const CMstring&) const; TCHAR operator[] (int nIndex) const; TCHAR& operator[] (int nIndex); CMstring& operator += (LPCTSTR pStr); CMstring& operator += (TCHAR ch); friend CMstring operator+(const CMstring& str1, const CMstring& str2); friend CMstring operator+(const CMstring& str1, LPCTSTR lpszstr); friend CMstring operator+(const CMstring& str1, TCHAR ch); friend CMstring operator+(TCHAR ch, const CMstring& str1); friend CMstring operator+(LPCTSTR lpszstr, const CMstring& str1); // friend wostream operator <<(wostream &is;,const CMstring &str;); public: LPCTSTR GetData() const; bool IsEmpty() const; TCHAR GetAt(int) const; TCHAR& GetAt(int); int GetLength() const; int Compare(const CMstring&) const; int CompareNoCase(const CMstring&) const; int Find(TCHAR ch, int nStart = 0) const; int Find(LPCTSTR pStr, int nStart = 0) const; int ReverseFind(TCHAR ch) const; int ReverseFind(LPCTSTR pStr) const; CMstring Right(int nCount) const; CMstring Left(int nCount ) const; public: CMstring& MakeLower(); CMstring& MakeUpper(); CMstring& MakeReverse(); int Replace(TCHAR chOld, TCHAR chNew); int Replace(LPCTSTR pszOld, LPCTSTR pszNew); int Insert(int iIndex, TCHAR ch); void Format(LPCTSTR lpszFormat, ...); private: CMStringImp* m_pImp; };

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值