C Program to Concatenate Two Strings

C program to concatenate or join to string; Through this tutorial, we will learn how to concatenate or join two string using for loop, while loop, function and pointer in c program.

Programs and Algorithm to Concatenate Two Strings in C

Let’s use the following algorithm and program to concatenate or join two string using for loop, while loop, function and pointer in c:

  • Algorithm To Concatenate Two Strings
  • C program to Concatenate Two Strings using For Loop
  • C program to Concatenate Two Strings While Loop
  • C program to Concatenate Two Strings using Function
  • C program to Concatenate Two Strings uisng Pointer

Algorithm To Concatenate Two Strings

Use the following algorithm to write a program concatenate or join two string; as follows:

  • START
  • Step 1 -> Take two string as str1 and str2.
  • Step 2 -> Move to the last position of first string let it be i.
  • Step 3 -> Now Loop over second string with j = 0
  • Assign the character str1[i] = str2[j]
  • Increment i and j and repeat till the last element of j.
  • End Loop
  • Step 3 -> Print str1.
  • STOP

C program to Concatenate Two Strings using For Loop

/* 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;
}

The output of the above c program; as follows:

Please Enter the First String :  hello
Please Enter the Second String :  world
String after the Concatenate = helloworld

C program to Concatenate Two Strings 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;
}

The output of the above c program; as follows:

Please Enter the First String :  hello
Please Enter the Second String :  dear
String after the Concatenate = hellodear

C program to Concatenate Two Strings using Function

#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';
}

The output of the above c program; as follows:

Please Enter the First String :  hello
Please Enter the Second String :  world
String after the Concatenate = helloworld

C program to Concatenate Two Strings uisng Pointer

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

The output of the above c program; as follows:

Please Enter the First String :  hello
Please Enter the Second String :  world
String after the Concatenate = helloworld

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *