C program to concatenate two strings – In this article, we will brief in on the multiple ways to concatenate two things in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The ways used in this piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
- Using String Library Function
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
As it is evident with the image uploaded above, we need to enter both the strings which we need to concatenate or link.
Both the strings that have been entered are as follows:
string1: hello
string2: world
Linking both the strings will get the new string to be:
helloworld
Thus, the multiple ways to do so in C programming are as follows:
Using Standard Method
- We are combining the two strings into one string.
2) Read the entered two strings using gets() function as gets(s1) and gets(s2).
3) Get the length of the string s1 using string library function strlen(s1) and initialize to j.
4) The for loop iterates with the structure for(i=0;s2[i]!=’\0′;i++) ,
append the characters of string s2 at s1[i+j] of the string s1 until there is no character is available in the string s2. Here we are adding the string s2 at the end of the string s1.
5) Print the concatenated string s1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> #include <string.h> int main() { char s1[1000],s2[1000]; int i,j; printf("Enter string1: "); gets(s1); printf("Enter string2: "); gets(s2); j=strlen(s1); for(i=0;s2[i]!='\0';i++) { s1[i+j]=s2[i]; } s1[i+j]='\0'; printf("combined two strings ='%s'\n",s1); return 0; } |
Output:
1 2 3 |
Enter string1: hello Enter string2: world combined two strings ='helloworld' |
Using Function
- The main() calls the stringconcatenate() function to combine the two strings.
2) The function gets the string s1 length using strlen(s1).
3) Append the character of string s2[i] at s1[i+j].Repeat this step by increasing i value until no character available in s2. Here, we append the characters of string s2 to s1 from the end of s1.
4) After all iterations of for loop, we will get concatenated string s1.
5) The main() prints the string s1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <stdio.h> #include <string.h> void stringconcatenate(char *s1,char *s2) { int i; int j=strlen(s2); for(i=0;s2[i];i++) { s1[i+j]=s2[i]; } s1[i+j]='\0'; } int main() { char s1[1000],s2[1000]; printf("Enter string1: "); gets(s1); printf("Enter string2: "); gets(s2); stringconcatenate(s1,s2); printf("combined two strings ='%s'\n",s1); return 0; } |
Output:
1 2 3 |
Enter string1: welcome to Enter string2: c beginners combined two strings ='welcome to c beginners' |
Using Recursion
- The function stringconcatenate() gets the string length of s1.
a) If no elements are available in s2 then assign s2 with a null character.
b) Otherwise, add the element of the string s2 at the end of the string s1 as s1[i+j]=s2[i]and increase the i value.
c) The function calls itself by passing modified string s1,s2 as arguments. It calls recursively until no elements are available in s2.
2)The main() prints the concatenated string s1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <stdio.h> #include <string.h> stringconcatenate(char *s1,char *s2) { static int i=0,j=strlen(s1); if(!s2[i]) { s2[i]='\0'; } else { s1[i+j]=s2[i]; i++; stringconcatenate(s1,s2); } } int main() { char s1[1000],s2[1000]; printf("Enter string1: "); gets(s1); printf("Enter string2: "); gets(s2); stringconcatenate(s1,s2); printf("combined two strings ='%s'\n",s1); return 0; } |
1 2 3 |
Enter string1: hello Enter string2: world combined two strings = 'helloworld' |
Using String Library Function
- The strcat(s1,s2) is a string library function available at the header file “string.h”.
2) The function strcat(s1,s2) combines the string s2 with s1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> #include <string.h> i int main() { char s1[1000],s2[1000]; printf("Enter string1: "); gets(s1); printf("Enter string2: "); gets(s2); strcat(s1,s2); printf("combined two strings ='%s'\n",s1); return 0; } |
1 2 3 |
Enter string1: welcome to Enter string2: c beginners combined two strings = 'welcome to c beginners' |