All lessons

Working with Text

7 min · C++

Join std::string values, measure them, and change case.

C++ · Working with Text0/5 cleared

Enemy 1 of 5

std::string and concatenation

This code joins the text Hello, and the text Ada using the + sign and prints the result. Press Run to see the combined greeting.

Why this works

C++ text lives in the string type, which comes from the <string> header (usually written std::string, but using namespace std; lets you drop the std:: prefix). Unlike a raw C-style character array, std::string manages its own memory, so it can grow, shrink, and be compared with == without you writing any low-level bookkeeping code.

The + operator joins (concatenates) two strings into a new one. This is the same + symbol used for adding numbers, but C++ figures out from the types involved that you mean "glue these pieces of text together" rather than "add these numbers."

You
Packet Wolf
Loading editor…
Sign up to fightThis one runs on our servers, so it needs a free account.