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++

Run it Now
#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 << "\nEnter 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 << "\nConcatenate String :" << str1;

    return 0;
}

Output:

Enter First String :Learn
Enter Second String :C++
Concatenate String :LearnC++