Tutorials  /  Linux Basics

String Concatenation in C++: A Complete Guide

Ccentron Redaktion · January 2024 ·4 min read ·Linux Basics, Tutorial

Contents

  1. Methods for String Concatenation in C++
  2. Recommendations and Best Practices
  3. Conclusion

This article explains various techniques for string concatenation in C++. String concatenation is a fundamental operation to combine two or more strings. Modern C++ versions offer optimized and efficient methods to achieve this.

Methods for String Concatenation in C++

C++ provides several techniques for string concatenation, including:

  • The + operator
  • The strcat() function (for char arrays)
  • The append() method (for std::string)
  • Using loops for manual concatenation
  • Advanced approaches with std::ostringstream

1. String Concatenation with the + Operator

The + operator is commonly used to combine two std::string objects.

Syntax:

Code
string1 + string2;

Example:

#include <iostream>

#include <string>

using namespace std;

int main() {

string str1 = "Hello, ";

string str2 = "World!";

string result = str1 + str2;

cout << "Concatenated String: " << result << endl;

return 0;

}

Code

Output:

Code
Concatenated String: Hello, World!

2. String Concatenation with the strcat() Method

strcat() is a function from the C standard library and is used with char arrays.

Syntax:

Code
strcat(char *dest, const char *src);

Example:

#include <iostream>

#include <cstring>

using namespace std;

int main() {

char str1[50] = "C++ ";

char str2[50] = "Programming";

strcat(str1, str2);

cout << "Concatenated String: " << str1 << endl;

return 0;

}

Code

Output:

Code
Concatenated String: C++ Programming

Note: Ensure that the destination string (dest) has enough space to hold the resulting string to avoid buffer overflows.

3. String Concatenation with the append() Method

The append() method is an efficient way to combine strings in modern C++.

Syntax:

Code
string1.append(string2);

Example:

#include <iostream>

#include <string>

using namespace std;

int main() {

string str1 = "Modern ";

string str2 = "C++";

str1.append(str2);

cout << "Concatenated String: " << str1 << endl;

return 0;

}

Code

Output:

Code
Concatenated String: Modern C++

4. String Concatenation Using a Loop

Manual concatenation can be performed using a loop, especially when working with char arrays.

Example:

#include <iostream>

using namespace std;

int main() {

char str1[50] = "Hello ";

char str2[50] = "World";

int i = 0, j = 0;

while (str1[i] != '\0') i++; // Find the end of the first string

while (str2[j] != '\0') {

str1[i++] = str2[j++]; // Copy characters one by one

}

str1[i] = '\0'; // Add null terminator

cout << "Concatenated String: " << str1 << endl;

return 0;

}

Code

Output:

Concatenated String: Hello World

5. Advanced Method: String Concatenation with std::ostringstream

std::ostringstream is particularly useful for efficiently combining many strings.

Example:

#include <iostream>

#include <sstream>

using namespace std;

int main() {

ostringstream oss;

oss << "Efficient " << "String " << "Concatenation";

string result = oss.str();

cout << "Concatenated String: " << result << endl;

return 0;

}

Output:

Concatenated String: Efficient String Concatenation

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €4.04 per month, billed by the hour and ready in seconds. Rent a cloud server →

Recommendations and Best Practices

  1. Prefer std::string over char arrays:

    std::string is safer as it automatically manages memory.

  2. Use std::ostringstream for complex concatenations:

    Ideal for scenarios involving the combination of multiple strings.

  3. Be mindful of memory:

    Ensure the destination string has enough space when working with char arrays.

  4. Error handling:

    Validate inputs, especially when accepting strings from users.

Conclusion

This updated guide has explored various techniques for string concatenation in C++. Each method has its own pros and cons, depending on the use case. Modern approaches like append() and std::ostringstream offer greater safety and efficiency and should be preferred for most applications.

Claim €200 credit

Test your setup on ccloud³

Sign up for ccloud³ and receive €200 starting credit for your project.

centron Redaktion Technical editorial team

The centron editorial team writes guides from day-to-day operations: tested on our own platform, run in our data centre in Hallstadt near Bamberg.

Category Linux Basics
Share
Still have questions?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

Was this tutorial helpful?

Your answer is stored anonymously and helps us improve our tutorials.

Comments

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Get started for free

Sign up and receive €200 credit at centron within your first 60 days.

This promotional offer applies to new accounts only. Available exclusively to businesses.