C program to Concatenate Two Strings

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

C program to Concatenate Two Strings without using strlcat()

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

/* C program to Concatenate Two Strings without using strcat() */

#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);

        // 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 String after the Concatenate = %s", Str1);
  	
  	return 0;
}
 Please Enter the First String :  Hello

 Please Enter the Second String :  World

 String after the Concatenate = HelloWorld

C program to Concatenate Two Strings Using While Loop

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

/* C code to Concatenate Two Strings without using strcat() */

#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

program to Concatenate Two Strings Using Functions

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

/* C program to Concatenate Two Strings without using strcat() */

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

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

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

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

void concatenate(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 String : C programming

 Please Enter the Second String :  Tutorial

 String after the Concatenate = C programmingTutorial

Program to Concatenate Two Strings Using Pointers

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

/* C code to Concatenate Two Strings without using strcat() */

#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 String :  ");
  	gets(Str1);
  	
  	printf("\n Please Enter the Second String :  ");
  	gets(Str2);
  	
  	while(* ++s1);
  	
  	while(*(s1++) = (*s2++)); 
  	
  	printf("\n String after the Concatenate = %s", Str1);
  	
  	return 0;
}
 Please Enter the First String :  Write a 

 Please Enter the Second String :  C Program

 String after the Concatenate = Write a C Program

Program to Concatenate Two Strings Using Pointer Functions

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

/* C Code to Concate Two Strings without using strcat() */

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

void concatenate(char *, char *); 

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

  	printf("\n String 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 String :  Good

 Please Enter the Second String :  Morning All

 String after the Concatenate = GoodMorning All