C Program to Concatenate two Strings using Arrays

C Program to Concatenate two Strings using Arrays

820
1
SHARE

C Program to Concatenate two using Arrays : There are lot of ways of concatenating strings in , also there is standard function in C “strcat” to concatenate the strings. But how to concatenate strings without using standard String function. Today we will learn how to concatenate two strings using arrays. Actually concept is quite easy, we just need to declare three string arrays, first two for taking input from user and third one to store concatenated string.

Logic to concatenate two strings : Say str1[50] and str2[50] are two input string arrays and str[100] will store concatenated string. Now we will store str1[50] into str[100] until null string ‘\0’ is encountered and then from next location start storing str2[50] until null string ‘\0’ is encountered.

Now let us see how to write a .

C Program to Concatenate two Strings using arrays :

#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
char str1[50],str2[50],str[100];
int i=0,j=0,k=0;
printf ("Enter first string : ");
gets (str1);
printf ("\n\nEnter second string : ");
gets (str2);
while (str1[i]!='\0')
{
str[k++]=str1[i++];
}
while (str2[j]!='\0')
{
str[k++]=str2[j++];
}
str[k]='\0';
printf("\n\nThe concatenated string is : ");
puts (str);
getch();
}

 Output of the program :

Enter first string : Lokesh is owner of

Enter second string : Ccodechamp

The concatenated string is : Lokesh is owner of Ccodechamp

We hope you all have enjoyed the using arrays. If you have any doubt ask us in form of comments.

1 COMMENT

  1. Can u pls explain these lines of the code

    while
    (str1[i]!=
    ‘\0’
    )
    {
    str[k++]=str1[i++];
    }
    while
    (str2[j]!=
    ‘\0’
    )
    {
    str[k++]=str2[j++];
    }
    str[k]=
    ‘\0’
    ;

LEAVE A REPLY