Concatenate String In C
In C, adding two strings together or updating them is known as string concatenation. In C, we can concatenate strings together to update them or add more than two strings together.
Concatenation makes it possible to combine two or more strings into a single string value.
String:
A one-dimensional array of characters that ends with the null ('0') character is known as a string. Strings are collections of characters that are encased in double quotation marks and, by default, finish with the null character (0).
Concatenation:
The joining of two items together is referred to as concatenation. It combines two things end to end, or you could say that it adds the second item to the first thing to get a new outcome.
There are various kinds in implementing the string concatenation
Implementation of string concatenation Using Loop:
#include <stdio.h>
int main()
{
char s1[20]; // declaration of char array variable
char s2[20]; // declaration of char array variable
int i; // integer variable
printf("Enter name1: ");
scanf("%s",s1);
printf("\nEnter name2: ");
scanf("%s",s2);
// obtaining the final component of the first array
for(i=0;s1[i]!='\0';i++); { for(int j=0;s2[j]!='\0';j++) { s1[i]=s2[j]; i++; } } // adding a null character to the char array's end s1[i]='\0'; printf("The new concatenated string will be after concatenation : %s", s1); return 0; }
OUTPUT:
![Concatenate String In C](https://static.tutorialandexample.com/c/concatenate-string-in-c1.png)
Explanation:
In the preceding illustration, we shown how to combine two strings using a loop. The user enters values for the two strings, s1 and s2, which are stated. We then joined these two values together and saved them in the first string, which we labelled s1.
Initializing a loop causes it to run from position i = 0 until it encounters the null character of the s1 character array. The value of 'i' will be one longer than the actual length of the string once the loop has finished running.
Once more, a fresh loop will be started, and each iteration will add one character. The iteration will keep going until name2's null character is found. In this manner, the string concatenation takes place using a loop.
Implementation of string concatenation Using Pointer:
#include <stdio.h>
int main()
{
char string1[25]; // declaration of the char array
char string2[25]; // declaration of the char array
int i=0,j=0; // integer variables
char *str1; //declaration of the char pointer
char *str2; //declaration of the char pointer
str1=string1;
str2=string2;
printf("Enter the first string: ");
scanf("%s",string1);
printf("\nEnter the second string: ");
scanf("%s", string2);
while(string1[i]!='\0')
{
++str1;
i++;
}
while(string2[j]!='\0') { *str1=*str2; str1++; str2++; j++; } printf("The new concatenated string will be after concatenation : %s",string1); return 0; }
OUTPUT:
![Concatenate String In C](https://static.tutorialandexample.com/c/concatenate-string-in-c2.png)
Explanation:
Two-character array variables, string1 and string2, are declared in the example above. These variable strings contain the value of the user's input. Then str1 and str2 are specified as two char pointers.
After storing the addresses of strings 1 and 2 in str1 and str2, a loop will run until string 1's null character is encountered. Then, the pointer will be at the position immediately following the string's final character.
Once more, a fresh loop will keep running until string 2's final character is reached. The character kept in the string's **j**th location is added to the **i**th location after each iteration. In this manner we implement the concatenation using pointers.
Implementation of string concatenation Using Strcat() function:
The simplest method for joining two strings in C is to use the strcat() function.Two arguments are required for this function. "Destination string" is the first, and "source string" is the second.
The outcome will be the concatenation of the two strings and their storage in the final string.
Syntax:
char *strcat(char *string_1, char *string_2);
#include <stdio.h> #include <string.h> int main() { char string1[25]; //declaration of the char array char string2[25]; //declaration of the char array printf("Enter the first string : "); scanf("%s", string1); printf("\nEnter the second string : "); scanf("%s",string2); strcat(string1,string2); // concatenation function printf("The new concatenated string will be after concatenation: %s",string1); return 0; }
OUTPUT:
![Concatenate String In C](https://static.tutorialandexample.com/c/concatenate-string-in-c3.png)
Explanation:
There are two-character arrays declared in the example above. 'string1' and'string2' are used to hold the values of strings that the user has entered as input. The values of strings 1 and 2 are then combined using the strcat() function, and the outcome is saved in the target string. The output will be the string that has been concatenated.
Implementation of string concatenation without Using Strcat() function:
Even if there are numerous ways to combine strings, we can manually combine two strings without the aid of a function or other approach. It stores the user's selected values in strings 1 and 2, then extracts the resulting string. The string_1 is placed into the resultant string first, followed by the string_2, in that order.
#include <stdio.h>
#include <string.h>
int main()
{
char string_1[100], string_2[100];
int i, j;
printf("Enter the first string: ");
gets(string_1);
printf("Enter the second string: ");
gets(string_2);
for (i = 0; string_1[i]!='\0'; i++); for (j = 0; string_2[j]!='\0'; j++, i++) { string_1[i] = string_2[j]; } string_1[i] = '\0'; printf("The new concatenated string will be after concatenation: %s\n", string_1); return 0; }
OUTPUT:
![Concatenate String In C](https://static.tutorialandexample.com/c/concatenate-string-in-c5.png)
Explanation:
We can see how to concatenate strings without using the strcat() function in the example above. The user enters the value of the strings, which are then stored in "string_1" and "string_2."
Both the first string's value and the second string's value are placed into new strings, respectively. The loop increases the value of i by 1 and determines if string_1[i] does not equal "0" or not.
Following this, a while loop is used to determine whether string [i] does not equal "0." The loop will be carried out if the condition turns out to be true. The output of the concatenated strings is then printed using printf.
Conclusion:
- Strings are a flexible, two-dimensional array that stores text and can be used in various ways. The process of adding two strings end to end to create a new string is known as string concatenation in the C programming language.
- When concatenating strings with a loop, the loop iterates over the first string until it encounters the first string's null character. The second string's characters are now added one by one to the end of the first string.
- Using pointers, we may concatenate strings by declaring two pointers that will store the addresses of the strings. At the conclusion of the loops, we enter the characters of the second string one at a time. The loops are run until they reach the null character of the first string.
- Using the strcat() function, two strings can also be appended. The user-provided values are simply added by this function, which then displays a new result.
- Without using the strcat() function, we may alternatively combine two strings, however this method requires using the include command to include the stdio header file.