codescracker
c

C Program to Concatenate Two Strings



« Previous Program Next Program »

In this tutorial, we will learn about how to concatenate two strings with and without using library function. Let's first start with concatenating string using library function.

using Library Function

To concatenate or to append one string into another string in C programming, you have to ask from the user to enter any two string and concatenate both the string using strcat() function as shown here in the program given below. The function takes two argument, that is strcat(str1, str2). Here string value of str2 will be appended at the end of string str1.

// Concatenate String using strcat() in C
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str1[50], str2[50];
    printf("Enter first string: ");
    gets(str1);
    printf("Enter second string: ");
    gets(str2);
    strcat(str1, str2);
    printf("\nString after concatenation is:\n%s", str1);
    getch();
    return 0;
}

The program was written under Code::Blocks IDE, therefore after successful build and run, you will get the following output:

c program concatenate two strings

Now supply the value of first string say codes and press ENTER key, then supply the string value for second string say cracker and finally press ENTER key to see the concatenated string as shown in the snapshot given here:

string concatenation c

The value of second string cracker (in this case) gets copied into the first string codes and the concatenated string will become codescracker as shown in the output. If we use the function strcat() as strcat(str2, str1), then the string value of str1 will gets copied to str2. Therefore if we supply codes for str1 and cracker for str2. Then strcat(str2, str1) will output the concatenated string as crackercodes.

without using strcat() Function

Now let's create another program that will also concatenate string but this time without using any library function

// String Concatenation without strcat() Function
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    char strOne[50], strTwo[50], i, count=0;
    printf("Enter first string: ");
    gets(strOne);
    printf("Enter second string: ");
    gets(strTwo);
    for(i=0; strOne[i]!='\0'; i++)
        count++;
    for(i=0; strTwo[i]!='\0'; i++)
    {
        strOne[count] = strTwo[i];
        count++;
    }
    strOne[count] = '\0';
    printf("\nString after concatenation is:\n%s", strOne);
    getch();
    return 0;
}

The program is created in such a way, that the value of second string (strTwo) will gets concatenated into the first string (strOne). And the program will print the value of concatenated string as output as shown in the sample run given below:

c string concatenation

Program Explained

Concatenate Two String into Third

Here is another program that will read any two string from the user and will concatenate into the third string without using any library function:

// Concatenate Two Strings in C
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    char strOne[50], strTwo[50], strCon[100], i, count=0;
    printf("Enter first string: ");
    gets(strOne);
    printf("Enter second string: ");
    gets(strTwo);
    for(i=0; strOne[i]!='\0'; i++)
    {
        strCon[i] = strOne[i];
        count++;
    }
    for(i=0; strTwo[i]!='\0'; i++)
    {
        strCon[count] = strTwo[i];
        count++;
    }
    strCon[count] = '\0';
    printf("\nString 1 = %s", strOne);
    printf("\nString 2 = %s", strTwo);
    printf("\nConcatenated String = %s", strCon);
    getch();
    return 0;
}

After successful build and run, here is the first snapshot of the sample run:

c concatenate string without library

Provide any two string say codes and cracker, then press ENTER key to see the three strings, first and second string is the string, user has provided and then the third string will be the concatenated string. Here is the second snapshot of the sample run:

concatenate string without library c

Same Program in Other Languages


« Previous Program Next Program »