Concatenate One String Contents To Another

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
using namespace std;

int main( )
{
    char str1[80], str2[80];

    cout<<"Enter first string: ";
    cin.getline(str1, 80);
    
    cout<<"Enter second string: ";
    cin.getline(str2, 80);

    int l = 0; //Hold length of first string
    
    //Find length of first string.
    for(l = 0; str1[l] != '\0'; l++);

    //Adding second string content in first
    for(int i = 0; str2[i] != '\0'; i++)
    {
        str1[l++] = str2[i];
    }
 
    str1[l] = '\0';

    cout << "\nThe first string after adding second string content:\n\n" << str1;   
 
    return 0;
}

Post a Comment

 
Top