Copy and Addition (Concatenation) of string class based strings – CSKC
Sign In

Search

Code Learning #CPP#3028    siteicon   siteicon  

Copy and Addition (Concatenation) of string class based strings

A demonstration program to see how string objects of string class are copied or concatenated one after the another.

Learning Objectives

  • Learning to copy and concatenation of string objects.

Source Code

TC++ #3028

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

  Related ( ? ) :

Code Understanding

string s1,s2,s3;
Three string class objects are declared;

s1=”Welcome”;
s1 instance is assigned a string literal.

s2=s1;
The contents of s1 are copied to s2

s3=s2+” to the world of c++”;
contents of s2 are concatenated with another string literal

cout<<s3<<endl;
Final concatenated string is printed

Notes

  • While concatenation string class based objects do not need care regarding the size of destination string as for such objects sizes are dynamically allocated by the compiler itself. This is a major advantage of using string class as against c-style null terminated strings.


Suggested Filename(s): stringcl-copy-concat.cpp



Share

Created: 25-Jan-2018 23:44 | Updated: 25-Jan-2018 23:48|