cpp / beginner
Snippet
String Concatenation and Methods
Strings can be combined using the + operator. The length() method returns the number of characters. String is a class from the Standard Library that provides many useful methods for text manipulation.
snippet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>#include <string>using namespace std;int main() {string first = "Hello";string second = "World";string combined = first + " " + second;cout << combined << endl;cout << "Length: " << combined.length() << endl;return 0;}
Breakdown
1
#include <string>
Header required for std::string class
2
string combined = first + " " + second;
Concatenates strings with a space between them
3
combined.length()
Returns the total character count of the string