Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

Concatenate Two Strings in C++ Examples

|| 0

Concatenate Two Strings in C++ Examples
Stechies

What in String concatenation?

The process of appending /joining or adding two or more strings into one string is known as Concatenate in a programming language. Concatenate is a function provided in almost all programming language like C++, Java, Python etc.

String Concatenation in C++

In c++ programming we concatenate with the help of functions and operators as explained in the examples below. 

Methods of Concatenation in C++

Here are the various method to perform String Concatenation in C++

1) Concatenate String Using Standard Library Function

Note: These functions concatenate prior to given strings.

Example Program

#include<iostream>
#include<string.h>
using namespace std;

int main()
{
    // for the first string

    char st1[30]="It is a Good Example "; 

          //for second string

char st2[30]="to concatenate strings";

strcat(st1, st2);

        // to get the print out of it

          cout<<st1<<endl;

          return 0;

          }

Output

It is a Good Example to concatenate strings

2) Concatenate String Using Operator

Now we will see how to concatenate using operator  ‘+’.

Example Program

#include<iostream>

#include<string.h>

using namespace std;

int main()

{

    //string objects declartion

    string name1;

    string name2;

    string name3

    //input string in respective variable

    cout<<"Enter string 1 : "<<endl;

    cin>>name1;

    cout<<"Enter string2 : "<<endl;

    cin>>name2;

    //concatenate the string using "+"

    name3 = name1 + name2;

    //print the result

    cout<<name3<<endl;

    return 0;

}

Output

Enter string 1:
Raj
Enter string2 
Kumar

Result

RajKumar

Concatenate String using std::stringstream

In this method, two or more string provided and concatenate into a final string for the output.

Example Code

#include <iostream>

#include <string>

#include <sstream>

int main()

{

    std::string stng1("jingle");

    std::string stng2("---");

    std::string stng3("bell");

    std::stringstream final;

    final << stng1 << stng2 << stng3;

    std::string s = final.str();

    std::cout << s;

    return 0;

}

Output 

jingle---bell 

Concatenate String using std::string::append

In this method, two or more string provided and concatenate into final string for the output, but it is differently added next string at end of first where the append function is used.

Example Program 

#include <iostream>

#include <string>

#include <sstream>

int main()

{

    std::string stng1("computer");

    std::string stng2(" ");

    std::string stng3("generation");

    std::string s;

    s.append(stng1);

    s.append(stng2);

    s.append(stng3);

    std::cout << s;

    return 0;

}

Output 

computer generation

Concatenate String using Strncat

this helps to concatenate two strings where one string remain original and the other will be appended to source one till the defined length if the length is less than defined number then it terminated at null. 

Example Program

Parameters defined in this example

  • String 1 should be enough length to hold the targeted string also for the null characters.
  • String 2 is a string which copied to string1.
  • Number: It is the number which defined the length how long the string to be appended from string 2.
/* Example of strncat */

#include <stdio.h>

#include <string.h>

int main ()

{

  char st1[40];

  char st2[15];

  strcpy (st1," This is strncat ");

  strcpy (st2,"Example");

  strncat (st1, st2, 6);

  puts (st1);

  return 0;

}

Output

This is strncat Exampl

Explanation of this example 

As see the output shows whole string except the example is only printed ‘Exampl’ e is missing as we mentioned string 2 takes upto 6 characters so that.

Summary

The Article is about the Concatenate operation where joining two or more strings are added or can add it to another string. It can do in multiple ways like explained in the above examples. Concatenate of strings can be done in a simple way and adding it with conditional way too (where we can define the length of string). As we tried to discuss every aspect of  Concatenate function in this article to explain our user the process of concatenation with the help of functions and operators.


Advertisement
Advertisement
Advertisement