字符串拼接strcpy

#include <iostream>
#include<string.h>
using namespace std;

class MyString{

private:

    char str[1024];

    unsigned len;/*字符串长度*/

public:

    MyString();

    MyString& operator=(const char *mstr)/*常量字符串赋值*/
    {
        len=strlen(mstr);
        strcpy(str,mstr);
        return*this;
    }

    MyString& operator=(const MyString mstr)/*同类型赋值*/
    {
        strcpy(str,mstr.str);
        len=mstr.len;
        return*this;
    }

    MyString operator+(const MyString mstr)/*字符串拼接*/
    {
        int l=len;
        l=l+strlen(mstr.str);
        char*s=new char [l];
        strcpy(s,str);
        strcpy(s+len,mstr.str);
        MyString m;
        m=s;
        return m;
    }

    char &operator [](int i)/*查询串内字符*/
    {
        return str[i];
    }

    bool operator==(const MyString mstr)/*判断是否相等*/
    {
        if(strcmp(str,mstr.str)==0)return true;
        else return false;
    }

    bool operator!=(const MyString mstr)/*判断是否不等*/
    {
        if(strcmp(str,mstr.str)==0)return false;
        else return true;
    }

    void operator+=(const MyString mstr)/*字符串拼接赋值*/
    {

         strcpy(str+len,mstr.str);
         len+=mstr.len;
    }

    bool operator>(const MyString mstr)/*按典序比较*/
    {
        if(strcmp(str,mstr.str)>0)return true;
        else return false;
    }

    bool operator<(const MyString mstr)/*同上*/
 {
        if(strcmp(str,mstr.str)<0)return true;
        else return false;
    }

    friend ostream &operator<<(ostream &out,const MyString mstr)/*友元输出流符号重载*/
{
    for(int i=0;i<(int)mstr.len;i++)
    {
        out<<mstr.str[i];
    }
    return out;
}
};

    MyString::MyString()
    {
        len=0;

    }

int main()

{

    char ts[1024];

    cin>>ts;

    MyString s;

    s = ts;

    cin>>ts;

    MyString ss;

    ss = ts;

    if(ss == s)

        cout<<"equal"<<endl;

    if(ss != s)

    {

        cout<<"Not equal"<<endl;

        if(s > ss)

            cout<<"s da yu ss"<<endl;

        if(s < ss)

            cout<<"s xiao yu ss"<<endl;

    }

    ss += s;

    MyString sss;

    sss = s + ss;

    sss[0] = '$';

    cout<<sss<<endl;

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值