Concatenate Two String C Program

By | November 21, 2015

C Program To Concatenate Two Strings

Learn How To Concatenate Two Strings in C Programming Language. Learn how to concatenate strings without strcat() Library function as well as using Pointers.

A String is basically an Array of Characters indicated by ‘\0’ as its terminating character.

Method 1: C Program Code To Concatenate Two Strings using strcat() Library Function

#include<stdio.h>
#include<string.h>

int main()
{
        char string_a[20];
        char string_b[20];
        printf("Enter the First String:\t");
        scanf("%s", string_a);
        printf("\nEnter the Second String:\t");
        scanf("%s", string_b);
        strcat(string_a, string_b);
        printf("\nFirst String:\t%s\n", string_a);
        printf("\nSecond String:\t%s", string_b);
        printf("\n");
        return 0;
}

Method 2: C Program To Concatenate A String without using strcat() and with Pointers

#include<stdio.h>

void strcatfunc(char *, char *);

int main()
{
        char string_a[20], string_b[20];
        printf("\nEnter the First String:\t");
        scanf("%s", string_a);
        printf("\nEnter the Second String:\t");
        scanf("%s", string_b);
        strcatfunc(string_a, string_b);
        printf("\nFirst String:\t%s\n", string_a);
        printf("\nSecond String:\t%s\n", string_b);
        return 0;
}

void strcatfunc(char *ptr1, char *ptr2)
{
        while(*ptr1 != '\0')
        {
            ptr1++;
        }
        while(*ptr2 != '\0')
        {
            *ptr1 = *ptr2;
            ptr1++;
            ptr2++;
        }
        *ptr1 = '\0';
}

Output

C Program To Concatenate Two Strings without using strcat and using Pointers

If you have any compilation errors or doubts in this C Program To Concatenate Strings using Pointers, let us know about in the Comment Section below.

One thought on “Concatenate Two String C Program

  1. Damodar Patil

    Is Strings important for interviews? Or should I only focus on Pointers and Data Structures?

    Reply

Let's Discuss