Write a C++ program to concatenate one string contents to another.

Here, to concat two string we attach second string at the end of first string and for that we need to find the length of first string.

Program Code:

#include<iostream>
using namespace std;
int main( )
{
int i, j, length = 0;      //This variable is to hold length of first string
char name1[30], name2[30];
cout<<"Enter your first string: ";
cin.getline(name1, 30);
cout<<endl;
cout<<"Enter your second string: ";
cin.getline(name2, 30);
cout<<endl;
for(i = 0; name1[i] != '\0'; i++) //This loop is to find length of first string.
{
length = length + 1; 
}
for(j = 0; name2[j] != '\0'; j++) //This loop is to add second string in first string
{
name1[length++] = name2[j];
}
name1[length] = '\0';
cout << "\n Concatenation of first & second string : " ;
cout<< name1;
return 0;
}

The output of this program is tested on www.jdoodle.com

Output:
Enter your first string: Mukesh Kumar
Enter your second string: Suruchika
Concatenation of first & second string: Mukesh KumarSuruchika


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: