Practical training c ++ language design-connection between two strings

Source: Internet
Author: User

To program the connection between two strings, you must use a character array to save the strings without using system functions.
Algorithm: First define two character arrays in the main function. The last character of the character array is 0, and it is treated as the termination condition of the loop, first point a pointer to the last character of the first string, and then copy the characters in the second string to the first string in sequence.
Code:
# Include <iostream. h>
Void main ()
{
Char A [20], B [10];
Cout <"Enter the value of the two character Arrays:" <Endl;
Cin>;
Cin> B;
For (INT I = 0; I! = '/0'; I ++)
For (Int J = 0; (a [I] = B [J])! = '/0'; I ++, J ++)
Cout <"after the two strings are connected:" <Endl;
Cout <A <Endl;
}

Use the string class to define the string object and implement the previous question again
Algorithm: the string class can be used as a special character array. You only need to define two objects in the main function and then use the overloaded operator + = to complete the connection function.
Code:
# Include <iostream>
# Include <string>
Using namespace STD;
Void main ()
{
String A, B;
Cout <"Enter these two string types:" <Endl;
Cin> A> B;
A + = B;
Cout <"value after connection:" <A <Endl;
}
 

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.