C program to Concatenate Two Strings

How to write a C program to Concatenate Two Strings without using the strcat function? In this Programming, We can concatenate two strings in multiple ways. But we will discuss four approaches for C string concatenation using For Loop, While Loop, Functions, and Pointers.

C program to Concatenate Two Strings without using strlcat()

This program allows users to enter two string values or character arrays. Next, it will use For Loop to iterate each character in that array and joins them (or concatenate them). Please refer strcat function as well.

#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str1[100], Str2[100];
  	int i, j;
 
  	printf("\n Please Enter the First String :  ");
  	gets(Str1);
  	
  	printf("\n Please Enter the Second :  ");
  	gets(Str2);

        // To iterate First String from Start to end  
  	for (i = 0; Str1[i]!='\0'; i++);

        // Concatenating Str2 into Str1  	
  	for (j = 0; Str2[j]!='\0'; j++, i++)
  	{
  		Str1[i] = Str2[j];
  	}
  	Str1[i] = '\0';

  	printf("\n After the Concatenate = %s", Str1);
  	
  	return 0;
}
 Please Enter the First String :  Hello

 Please Enter the Second :  World

 After the Concatenate = HelloWorld

C program for String Concatenation Using While Loop

This program for string concatenation is the same as above, but this time we are using While Loop (Just replacing Programming For Loop with While Loop).

/* using a while loop */
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char Str1[100], Str2[100];
  	int i, j;
 
  	printf("\n Please Enter the First String :  ");
  	gets(Str1);
  	
  	printf("\n Please Enter the Second String :  ");
  	gets(Str2);
 
  	i = 0;
	while( Str1[i]!='\0')
	{
		i++;
	}
	
  	j = 0;
  	while( Str2[j]!='\0')
  	{
  		Str1[i] = Str2[j];
  		i++;
  		j++;
  	}
  	Str1[i] = '\0';

  	printf("\n String after the Concatenate = %s", Str1);
  	
  	return 0;
}
C program to Concatenate Two Strings without using strcat 2

C program to Concatenate Two Strings using Functions

This program for string concatenation is the same as above. However, we are currently using the Functions concept to separate the logic from the main program.

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

void combine(char [], char []); 

int main()
{
  	char Str1[100], Str2[100];
 
  	printf("\n Please Enter the First :  ");
  	gets(Str1);
  	
  	printf("\n Please Enter the Second :  ");
  	gets(Str2);
  	
  	combine(Str1, Str2);

  	printf("\n After the Concat = %s", Str1);
  	
  	return 0;
}

void combine(char s1[], char s2[])
{
	int i, j;
	
	i = 0;
	while( s1[i]!='\0')
	{
		i++;
	}
	
  	j = 0;
  	while( s2[j]!='\0')
  	{
  		s1[i] = s2[j];
  		i++;
  		j++;
  	}
  	s1[i] = '\0';
}
 Please Enter the First : C programming

 Please Enter the Second :  Tutorial

 After the Concat = C programmingTutorial

C program for String Concatenation using Pointers

This string concatenation program is the same as the second example, but this time we are using the Pointers concept.

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

int main()
{
  	char Str1[100], Str2[100];
  	char *s1 = Str1;
	char *s2 = Str2;
 
  	printf("\n Please Enter the First :  ");
  	gets(Str1);
  	
  	printf("\n Please Enter the Second :  ");
  	gets(Str2);
  	
  	while(* ++s1);
  	
  	while(*(s1++) = (*s2++)); 
  	
  	printf("\n After the Concatenate = %s", Str1);
  	
  	return 0;
}
 Please Enter the First :  Write a 

 Please Enter the Second :  C Program

 After the Concatenate = Write a C Program

Concatenate using Pointer Functions

This program for string concatenation is the same as above. However, this time we pass pointers to Functions to separate the logic from the main program.

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

void concatenate(char *, char *); 

int main()
{
  	char Str1[100], Str2[100];
 
  	printf("\n Please Enter the First :  ");
  	gets(Str1);
  	
  	printf("\n Please Enter the Second :  ");
  	gets(Str2);
  	
  	concatenate(Str1, Str2);

  	printf("\n After the Concatenate = %s", Str1);
  	
  	return 0;
}

void concatenate(char *Str1, char *Str2)
{
	while(*Str1)
	{
		Str1++;
	}
	
	while(*Str2)
	{
		*Str1 = *Str2;
		*Str1++;
		*Str2++;
  	}
  	*Str1 = '\0';
}
 Please Enter the First :  Good

 Please Enter the Second :  Morning All

 After the Concatenate = GoodMorning All