The C++ append() program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ append() member function to add characters to the end of a string in C++ programming

To show: How to use the C++ append() member function to add characters to the end of a string in C++ programming

 

// the C++ append() program example

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

// appending a C-string to a string

string str1("Playing ");

const char *str2 = "with a string";

cout<<"str1 is: "<<str1<<endl;

cout<<"str2, C string is: const char *str2 = \"with a string\" = "<<str2<<endl;

str1.append(str2);

cout<<"Operation: str1.append(str2)"<<endl;

cout<<"Appending str2 to str1: "<<str1<<endl;

 

// appending part of a C-string to a string

string str3 ("Replaying ");

const char *str4 = "the string ";

cout<<"\nstr3 string is: "<<str3<<endl;

cout<<"str4 C-string is: "<<str4<<endl;

str3.append(str4, 6);

cout<<"Operation: str3.append(str4, 6)"<<endl;

cout<<"Appending part of the str4 to string str3: "<<str3<<endl;

 

// appending part of one string to another

string str5("Again "), str6("string manipulation");

cout<<"\nstr5 is: "<<str5<<endl;

cout<<"str6 is: "<<str6<<endl;

str5.append(str6, 4, 6);

cout<<"Operation: str5.append(str6, 4, 6)"<<endl;

cout<<"The appended string is: "<<str5<<endl;

 

// appending one string to another in two ways, comparing append and operator [ ]

string str7("First "), str8("Second "), str9("Third ");

cout<<"\nstr7 is: "<<str7<<"\nstr8 is: "<<str8<<"\nstr9 is: "<<str9<<endl;

str7.append(str8);

cout<<"Operation: str7.append(str8)"<<endl;

cout<<"The appended string str7 is: "<<str7<<endl<<endl;

str7 += str9;

cout<<"Operation: str7 += str9"<<endl;

cout<<"The re-appended string is: "<<str7<<endl;

 

// appending characters to a string

string str10("What string");

cout<<"\nstr10 string is: "<<str10<<endl;

str10.append(3, '?');

cout<<"Operation: str10.append(3, '?')"<<endl;

cout<<"str10 string appended with ? is: "<<str10<<endl;

 

// appending a range of one string to another

string str11("Finally "), str12("comes the END ");

cout<<"\nstr11 is: "<<str11<<" str12 is: "<<str12<<endl;

str11.append(str12.begin() + 6, str12.end() - 1);

cout<<"Operation: str11.append(str12.begin() + 6, str12.end() - 1)"<<endl;

cout<<"The appended str11 String is: "<<str11<<endl;

 

return 0;

}

 

Output example:

 

str1 is: Playing

str2, C string is: const char *str2 = "with a string" = with a string

Operation: str1.append(str2)

Appending str2 to str1: Playing with a string

 

str3 string is: Replaying

str4 C-string is: the string

Operation: str3.append(str4, 6)

Appending part of the str4 to string str3: Replaying the st

 

str5 is: Again

str6 is: string manipulation

Operation: str5.append(str6, 4, 6)

The appended string is: Again ng man

 

str7 is: First

str8 is: Second

str9 is: Third

Operation: str7.append(str8)

The appended string str7 is: First Second

 

Operation: str7 += str9

The re-appended string is: First Second Third

 

str10 string is: What string

Operation: str10.append(3, '?')

str10 string appended with ? is: What string???

 

str11 is: Finally str12 is: comes the END

Operation: str11.append(str12.begin() + 6, str12.end() - 1)

The appended str11 String is: Finally the END

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index