String class (II)

Description

Encapsulates a string class for storing strings and processing related functions, supporting the following operations:

1. STR::STR() constructor: Creates an empty string object.

2. STR::STR(const char *) construction method: Create a string object, the content of the string is given by the parameter.

3. STR::length() method: Returns the length of the string.

4. STR::putline() method: output the content of the string and wrap it in a new line.

5. The operators "+" and "+=" represent the concatenation of two strings. The rules are:

   c = a + b means that the characters in string c are the concatenation of a and b: the result of "a+b" is a new string with the contents of strings a and b unchanged.

   a += b means that the characters in string a are the concatenation of a and b: the contents of string b remain unchanged

-----------------------------------------------------------------------------

You design a string class STR so that the main() function can run correctly.

See append.cc for the function call format.

The main() function has been given in append.cc.

-----------------------------------------------------------------------------

Invalid Word error: "string", "vector", etc. are disabled.

Input

The input has several lines, each with a string.

Output

Each set of test data corresponds to a line of output, which contains two parts, the first is an integer representing the length of the input string, and the second is the input string, separated by a space. See sample for format.

Sample Input

A123456789

Sample Output

12 Hello World!
0
12 Hello World!
12 Hello World!
12 Hello World!
10 A123456789
1 A
9 123456789
10 123456789A
1 A


#include<bits/stdc++.h>
using namespace std;
void f(char *s1,char *s2,int length)//令s1=s2,length为s2的长度
{
    int i;
    for( i=0;i<length;i++)
        {
            s1[i]=s2[i];
        }
        s1[i]=0;
}
class STR
{
public:
    STR()
    {
       l=0;
    }
    STR(char *p)
    {
        int i;
        for(i=0;p[i]!=0;i++)
        {
        }l=i;
        s=new char[l+1];
        f(s,p,l);
    }
    ~STR()
    {
       delete []s;
    }
    int length()
    {
        return l;
    }
    void putline()
    {
        cout<<s;
        cout<<endl;
    }
    STR &operator+=(STR &p)//加&是因为没有写构造函数,成员有指针。
    {
        //思路是先构造一个q存左操作数,左操作数new完后,再将q和p复制过去。
        int j=l;
        STR q(s);
        l=l+p.l;
        delete []s;
        s=new char[l+1];
        f(s,q.s,q.l);
        for(int i=0;i<p.l;i++)
        {
            s[j++]=p.s[i];
        }
        s[l]=0;
        return *this;

    }
    friend STR operator+(STR &e,STR &h);

private:
    int l;
    char *s;
};
   STR operator+(STR &e,STR &h)
{
    STR temp;
    int j=e.l;
    temp.l=e.l+h.l;
    temp.s=new char[temp.l+1];
    for(int i=0;i<e.l;i++)
    {
        temp.s[i]=e.s[i];
    }
    for(int i=0;h.s[i]!=0;i++)
    {
        temp.s[j++]=h.s[i];
    }
    temp.s[temp.l]=0;
    return temp;
}
int main()
{
    STR e;
    STR h("Hello World!");
    STR he = e + h;
    cout << he.length() << " ";
    he.putline();
    cout << e.length() << " ";
    e.putline();
    cout << h.length() << " ";
    h.putline();
    e += h;
    cout << e.length() << " ";
    e.putline();
    cout << h.length() << " ";
    h.putline();

    char s1[100001], s2[100001];
    while(gets(s1) != NULL && gets(s2) != NULL)
    {
        STR str1(s1), str2(s2);
        STR str = str1 + str2;
        cout << str.length() << " ";
        str.putline();
        cout << str1.length() << " ";
        str1.putline();
        cout << str2.length() << " ";
        str2.putline();
        str2 += str1;
        cout << str2.length() << " ";
        str2.putline();
        cout << str1.length() << " ";
        str1.putline();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324616399&siteId=291194637