C++ Program to concatenate two strings using friend function


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


strcat(t.str1, t.str2);


Example: How to concatenate two strings using friend function in C++

Run it Now
#include<iostream>
#include<cstring>
using namespace std;

class Test {
private:
   char str1[100], str2[100];
public:

   void input() {
       cout << "Enter First  String :";
       cin.getline(str1, 100);

       cout << "\nEnter Second String :";
       cin.getline(str2, 100);
   }

   friend void add(Test t);
};

void add(Test t) {
   strcat(t.str1, t.str2);
   cout << "\nAfter Concatenated String :" << t.str1;
}

int main() {
   Test t;
   t.input();
   add(t);

   return 0;
}

Output:

Enter First  String :Learn
Enter Second String :C++
After Concatenated String :LearnC++