C++ Program to concatenate two strings without using strcat function


In this program, You will learn how to concatenate two strings without using strcat function in C++.


str1 = "learn", str2 = "java"

strcat(str1, str2);

New String: learnjava

Example: How to concatenate two strings without using strcat function in C++.

#include<iostream>
using namespace std;

int main() {

    char str1[100], str2[100];
    int i = 0, j = 0;

    cout << "Enter first string:";
    cin.getline(str1, 100);

    cout << "Enter second string:";
    cin.getline(str2, 100);

    while (str1[i] != '\0') {
        i++;
    }

    while (str2[j] != '\0') {
        str1[i] = str2[j];
        j++;
        i++;
    }
    str1[i] = '\0';

    cout << "Concatenated string:" << str1;

    return 0;
}

Output:

Enter first string:Xiith
Enter second string:.com

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.