C++ Program to Concatenate Two Strings Without Using String Function

In this program, the user is ask to enter the two strings, then the second string is concatenated (or attached) with first string.

The basic logic is that you have scan first string upto the last character, then read second string one by one and attach it to last of first string.

For example,

s1 = "Hello,"
s2 = "How are you?"

Then after string concatenation (means s1 + s2) you will get "Hello, How are you?".

You can use strcat() String function to do the same thing, here we have not used this function.

Source Code
using namespace std;

int main()
    char s1[40], s2[20];
    int i, j;
    cout<<"Enter first strings:";
    cin>> s1;
    cout<<"Enter second strings:";
    cin>> s2;

    // calculate the length of string s1
    // and store it in i
    for(i = 0; s1[i] != '\0'; ++i);

    for(j = 0; s2[j] != '\0'; ++j, ++i)
        s1[i] = s2[j];
    s1[i] = '\0';
    cout<<"After concatenation:\n "<< s1;
    return 0;
Enter first strings:Hello
Enter second strings:World!!
After concatenation: