Concatenating Strings in C
To concatenate strings in C, we can use the strcat function from the string.h library. The strcat function appends the source string to the destination string.
C Program to Concatenate Multiple Strings
The below example demonstrates the use of the strcat function to concatenate multiple strings in C.
// C program to concatenate multiple strings
#include <stdio.h>
#include <string.h>
int main()
{
// creating string variables
char str1[50] = "Hello";
char str2[50] = " w3wiki!";
char str3[50] = " How are you?";
printf("Initial strings:\n%s\n%s\n%s\n", str1, str2,
str3);
// Concatenating strings
strcat(str1, str2);
strcat(str1, str3);
printf("String after concatenation: %s\n", str1);
return 0;
}
Output
Initial strings: Hello w3wiki! How are you? String after concatenation: Hello w3wiki! How are you?
Time Complexity: O(n)
Auxiliary Space: O(1)
How to Concatenate Multiple Strings in C?
In C, concatenating strings means joining two or more strings end-to-end to form a new string. In this article, we will learn how to concatenate multiple strings in C.
Example:
Input:
char str1[50] = "Hello";
char str2[50] = " w3wiki";
Output:
Hello w3wiki!