Updated March 23, 2023
Introduction on String Concatenation in C
In the C Programming Language, the string concatenation is the process of joining/concatenating character strings from one end to another end. The strcat function joins the copy of string pointed by string_2 to the end of the string pointed by string_1 and it returns a pointer to string_1. For concatenation of string, we are using strcat function with the use of header function string.h. The process of concatenation is also referred to as binary addition of string with the use of + sign, for example, String + Concatenation = String Concatenation. Let’s see the syntax of the strcat function below,
Syntax:
char *strcat(char *string1, const char *string2);
The required header file for the strcat function is,
#include <string.h>
Here the parameters of the syntax explain that the string1 a pointer to string which will be altered and string2 will be copied to the end of string1, string2 a pointer to a string which will be added to the end of string1. Finally, the strcat function returns a result of a pointer to string1.
How Does String Concatenation Work in C?
In C-Programming the concatenation of string works with given strings as a single result with the use of strcat() function. The first and foremost thing is to include the header files required for the program which is pre-processor directive stdio.h and string.h, the header file string.h describes that variable type, macro, and several functions to operate arrays of characters in the program.
Code:
#include <stdio.h>
#include<string.h>
int main()
{
char str1[100], str2[100];
printf("First String: \n"); gets(str1);
printf("Second String: \n"); gets(str2);
strcat(str1,str2);
printf("Concatenation of both string is %s\n", str1);
return 0;
}
Code Explanation: The important function is the main() which is declared as integer to return the result as integer value at the termination of the program. In the main() function, we declare the character arrays as str1[] and str2[] which have the memory location. For the displaying purpose you have to use the printf() statements, the statement gets(str1) requires for fetching the characters as string and store it in arraystr1[]. Likewise the another gets(str2) for fetching another string and store it in array str2[]. Finally, with the use of strcat() function we concatenating the strings by following the syntax char * strcat(str1,str2) here the str1 defines the destination array it contains the string_1 and results the concatenated string, then str2 also contains string for concatenating, at the end of program it returns 0 as an integer type value. Finally, the output will be as shown below,
Output:
String Concatenation in C Using Various Methods
In C-Programming the finest approach to concatenate two strings is by using strcat() function. Let’s see the example to concatenate two strings manually without using strcat() function.
Example #1
Here, the strings string_1 and string_2 get concatenated and the final result is stored in string_1. The main thing that the length of string_1 should enough to store the string after the process of concatenation, otherwise you will get an unexpected result.
Code:
#include <stdio.h>
int main()
{
char string_1[100]="Code", string_2[]="Analysis";
int i,j;
for(i=0;string_1[i]!='\0';++i)
//to store length of string_1 in i
{
printf("i=%d\n",i);
}
// to concatenating characters of string_2 to string_1
for(j=0;string_2[j]!='\0';++j,++i)
{
string_1[i]=string_2[j];
}
// to finish string_1 string
string_1[i] = '\0';
printf("Result: Concatenation of Strings: ");
puts(string_1);
return 0;
}
Output:
Example #2
This program is used to concatenate two given strings by using the strlen() function. Have to include the string.h header file; it categorizes various functions to work with arrays of characters with the program. Then to define the main() function and declared int as return type at the end of the program. The inside the main() function, you have to take two character arrays name string_1[] and string_2[] to allocate a memory location.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char string_1[50] = "Programming";
char string_2[] = "Language";
int i, j, a,b;
a = strlen(string_1);
b = strlen(string_2);
j = 0;
for(i = a; i< a+b; i++ )
{
string_1[i] = string_2[j];
j++;
}
string_1[i] = '\0';
printf("%s", string_1);
return 0;
}
Output:
Example #3
This program is to concatenate two given strings using pointers. Previously the program describes the approach to concatenate two strings by various methods. In this below program, it takes two strings to concatenate and it stores with pointers actualString and toAppend. The function stringConcatenation() takes 2 parameters that one keeps as the reference and another one traces till the end. Finally, it appends both strings as a result.
Code:
#include <stdio.h>
void stringConcatenation(char*, char*);
int main()
{
char actualString[100], toAppend[100];
printf("Source String : \n");
gets(actualString);
printf("String to Concatenate : \n");
gets(toAppend);
stringConcatenation(actualString, toAppend);
printf("Result:\n"" String Concatenation: \"%s\"\n", actualString);
return 0;
}
void stringConcatenation(char *actualString, char *toAppend)
{
while(*actualString)
actualString++;
while(*toAppend)
{
*actualString = *toAppend;
toAppend++;
actualString++;
}
*actualString = '\0';
}
Output:
Example #4
The concatenation of two strings without using of strcat() function, the process of concatenation with the given two strings string_1, string_2 and the third-string string_3 is for storing the resultant concatenation strings. Finally, it displays the concatenated string.
Given Strings:
String_1="Welcome"
String_2="Strings"
Output: WelcomeStrings
Code:
#include <stdio.h>
int main()
{
// to get the two Strings to be concatenated
char string_1[100] = "String", string_2[100] = "Concatenation";
// to declare a new String for the storage purpose of the concatenated String
char string_3[100];
int i = 0, j = 0;
printf("\nFirst string: %s", string_1);
printf("\nSecond string: %s", string_2);
// to insert the first string in the new string
while (string_1 [i] != '\0') {
string_3[j] = string_1[i];
i++;
j++;
}
// to insert the second string in the new string
i = 0;
while (string_2[i] != '\0') {
string_3[j] = string_2[i];
i++;
j++;
}
string_3[j] = '\0';
// to print the concatenated string
printf("\nConcatenated string: %s", string_3);
return 0;
}
Output:
Conclusion
In C Programming, we learned about the String Concatenation method with and without the usage of strcat() function with the examples and how to make use of it. I hope the Concatenation of string with various methods would help out you with those examples.
Recommended Articles
This is a guide to String Concatenation in C. Here we discuss the syntax, working, and methods of String Concatenation in C along with the examples and code implementation. You may also look at the following articles to learn more –