codescracker
c++

C++ Program to Concatenate Two Strings



« Previous Program Next Program »

Here you will learn and get code on string concatenation in C++. The program to concatenate string is created in following ways:

String Concatenation without strcat()

This program concatenates or appends second string into first without using any library function like strcat():

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    char strOne[100], strTwo[50], i, j=0;
    cout<<"Enter the First String: ";
    gets(strOne);
    cout<<"Enter the Second String: ";
    gets(strTwo);
    for(i=0; strOne[i]!='\0'; i++)
        j++;
    for(i=0; strTwo[i]!='\0'; i++)
    {
        strOne[j] = strTwo[i];
        j++;
    }
    strOne[j] = '\0';
    cout<<"\nString after Concatenation: "<<strOne;
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program concatenate two strings

Now supply any string as input say codes and then another string as input say cracker. Press ENTER key to add the second string cracker to first one codes as shown in the output given below:

string concatenation without strcat c++

Here is another sample run with user input, this is as first string and codescracker as second string:

string concatenation c++

Note - Don't forgot to enter space after this is or before codescracker while entering the first or second string. Otherwise, both the string gets concatenated without space like this iscodescracker.

The following two statements:

strOne[j] = strTwo[i];
j++;

can also be replaced with:

strOne[j++] = strTwo[i];

That is, a post increment to j is used, that increments its value by 1 after evaluating the statement:
strOne[j] = strTwo[i];

If user enters first string as codes, then it gets initialized to strOne in a way that:

And if user enters cracker as second string, then it also gets initialized to strTwo in similar way. Therefore with this two string input, the dry run of above program goes like:

String Concatenation using strcat()

The function strcat() takes two strings as argument. The value of second string gets appended or concatenated to first string. For example, if there are two string say strOne and strTwo. And the value of both strings are:

strOne = "codes";
strTwo = "cracker";

Then,

strcat(strOne, strTwo);

Means now strOne = codescracker. That is, the value of second string concatenated to first one. This function is defined in string.h library

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    char strOne[100], strTwo[50];
    cout<<"Enter the First String: ";
    gets(strOne);
    cout<<"Enter the Second String: ";
    gets(strTwo);
    strcat(strOne, strTwo);
    cout<<"\nString after Concatenation: "<<strOne;
    cout<<endl;
    return 0;
}

This program will produce the same output as of previous one.

String Concatenation using + Operator

The question is, write a program in C++ to concatenates two strings using + operator. The answer to this question is given below:

#include<iostream>
using namespace std;
int main()
{
    string strOne="", strTwo="", strThree="";
    cout<<"Enter the First String: ";
    cin>>strOne;
    cout<<"Enter the Second String: ";
    cin>>strTwo;
    strThree = strOne + strTwo;
    cout<<"\nString after Concatenation: "<<strThree;
    cout<<endl;
    return 0;
}

Here is its sample run with user input, codes and cracker as first and second string:

string concatenation using + c++

Note - Previous program will only work with string input without any space.

String Concatenation using Pointer

This is the last program, that concatenates two strings using pointer.

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    char strOne[100], strTwo[50];
    char *firstString, *secondString;
    cout<<"Enter the First String: ";
    gets(strOne);
    cout<<"Enter the Second String: ";
    gets(strTwo);
    firstString = strOne;
    secondString = strTwo;
    while(*firstString)
        firstString++;
    while(*secondString)
    {
        *firstString = *secondString;
        firstString++;
        secondString++;
    }
    *firstString = '\0';
    cout<<"\nString after Concatenation: "<<strOne;
    cout<<endl;
    return 0;
}

Here is its sample run with user input, C++ Program to as first string and Concatenate Two Strings as second string:

string concatenation using pointer c++

The address of both the string strOne and strTwo gets initialized to its corresponding pointers, say firstString and secondString. And based on the address, we've concatenates the second string's value to the first one. To learn more about pointer in C++, you can follow its separate tutorial

Note - The * is called as value at operator

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker