In this C++ tutorial, you shall learn how to append a character to the end of a given string using Addition Assignment Operator, with example programs.
Append Character to end of String
To append character to end of a string in C++, we can use Addition Assignment Operator. Pass the string and character as operands, respectively, to Addition Assignment Operator. The expression updates the string, with the character appended to the end of the string.
The syntax of the statement to append a character ch
to the end of string str
is
str += ch;
C++ Program
In the following program, we take a string in str
, and a character in ch
. We append the character ch
to the end of the string str
, and print the resulting string to console output.
main.cpp
#include <iostream>
using namespace std;
int main() {
string str = "apple";
char ch = 'm';
str += ch;
cout << str;
}
Output
applem
Reference
In the above program(s), we have used the following C++ concepts. The links have been provided for reference.
Conclusion
In this C++ Tutorial, we learned how to append a character to the end of a given string.