String class, c ++ string class

Source: Internet
Author: User

String class, c ++ string class

Problem B: string class (II) Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 3495 Solved: 1504
[Submit] [Status] [Web Board] Description

Encapsulate a string class for storing strings and processing related functions. The following operations are supported:

 

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

2. STR: STR (const char *) constructor: Creates a String object. The content of the string is provided by parameters.

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

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

5. The operators "+" and "+ =" indicate the join operations of the two strings. The rule is:

C = a + B indicates that the characters in string c are connected by a and B: the result of "a + B" is a new string, and the content of string a and string B remains unchanged.

A + = B indicates that the characters in string a are connected by a and B: The content in string B remains unchanged.

 

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

 

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

 

For the function call format, see append. cc.

 

The main () function is provided in append. cc.

 

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

 

Invalid Word: "string", "vector", and so on are disabled.

 

 

Input

The input has several rows, one character string per line.

 

Output

Each group of test data corresponds to the output line, which contains two parts: an integer, indicating the length of the input string, and then the input string. The two are separated by a space. For the format, see sample.

 

Sample InputA123456789Sample Output12 Hello World! 0 12 Hello World! 12 Hello World! 12 Hello World! 10 A1234567891 A9 12345678910 123456789A1 AHINT Append Codeappend. cc, Problem D: string class (I) Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 3667 Solved: 1780
[Submit] [Status] [Web Board] Description

Encapsulate a string class for storing strings and processing related functions. The following operations are supported:

 

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

2. STR: STR (const char *) constructor: Creates a String object. The content of the string is provided by parameters.

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

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

 

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

 

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

 

For the function call format, see append. cc.

 

The main () function is provided in append. cc.

 

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

 

Invalid Word: "string", "vector", and so on are disabled.

 

 

Input

The input has several rows, one character string per line.

 

Output

Each group of test data corresponds to the output line, which contains two parts: an integer, indicating the length of the input string, and then the input string. The two are separated by a space. For the format, see sample.

 

Sample InputA123456789Sample Output0 12 Hello World! 1 A9 123456789 HINT Append Codeappend. cc,

# Include <iostream>
# Include <cstdio>
# Include <iomanip>
Using namespace std;
Class STR
{
Private:
Char * p;
Int len;
Public:
STR ()
{
P = new char [1];
P [0] = '\ 0 ';
Len = 0;
}
STR (const char * s)
{
Int I, j;
For (I = 0; s [I]! = '\ 0'; I ++ );
Len = I;
P = new char [len + 1];
For (j = 0; j <len; j ++)
{
P [j] = s [j];
}
P [j] = '\ 0 ';
}
Int length ()
{
Return len;
}
Void putline ()
{
Int I;
For (I = 0; I <len; I ++)
Cout <p [I];
Cout <endl;
}
//~ STR () {free (p );}
STR operator + (STR & str)
{
Int I = 0;
For (; str. p [I]! = '\ 0'; I ++ ){}
Int j = 0;
For (; p [j]! = '\ 0'; j ++ ){}
Char * pp = new char [I + j + 2];
For (int k = 0; k <j; k ++)
{
Pp [k] = p [k];
}
For (int k = 0; k <I; k ++)
{
Pp [j + k] = str. p [k];
}
Pp [j + I] = '\ 0 ';
Return STR (pp );
}
STR & operator + = (STR & str)
{
Int I = 0;
For (; str. p [I]! = '\ 0'; I ++ ){}
Int j = 0;
For (; p [j]! = '\ 0'; j ++ ){}
Char * pp = new char [I + j + 2];
For (int k = 0; k <j; k ++)
{
Pp [k] = p [k];
}
For (int k = 0; k <I; k ++)
{
Pp [j + k] = str. p [k];
}
Pp [j + I] = '\ 0 ';
* This = STR (pp );
Return * this;
}
};

The answer is applicable to these two questions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.