C Program to Concatenate Two Strings Using Pointers

C program to concatenate two strings using pointers. String concatenation is the operation of joining character strings end-to-end in formal language theory and computer programming. This will concatenate the pointer values in c.

#include <stdio.h>
int main()
{
char firstelement[100], secondelement[100];
printf("\nEnter the first string: ");
gets(firstelement); 
printf("\nEnter the second string to be concatenated");
gets(secondelement); 
char *a = firstelement;
char *b = secondelement;
while(*a) 
{
a++; 
}
while(*b) 
{
*a = *b;
b++;
a++;
}
*a = '\0'; 
printf("\n\n\nThe string after concatenation is: %s ", firstelement);
return 0;
}

 

Output:

Enter the first string: inlarn

Enter the second string to be concatenateddotcom

The string after concatenation is: inlarndotcom

Gets Function in C

The gets() function allows the user to enter a string of characters followed by the enter key. A character array is created to hold all of the characters entered by the user. To convert the array to a string, the null character is added. The user can enter space-separated strings using the gets() function.

Puts Function in C

In C/C++, the puts() function is used to write a line or string to the output (stdout) stream. It returns an integer value after printing the passed string with a newline. The value of the return is determined by the success of the writing procedure.

Pointer in C

In C, a pointer is a variable that stores the address of another variable. This variable can be of any type, including int, char, array, function, or pointer. The pointer’s size is determined by the architecture. However, in 32-bit architecture, a pointer is 2 bytes in size.

While Loop in C

A while loop is a control flow statement in most computer programming languages that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be viewed as a looping if statement. Using this program we can execute c program to concatenate two strings using pointers.