Like Us On Facebook

Follow Us On Twitter

Drop Down Menu

Tuesday 6 August 2013

C Code To Concatenate Two Strings

Hello guys,
In this post, I'll explain the C Code To Concatenate Two Strings. 

As we all know that, the process of appending one string to the end of another string is called concatenation. If s1 and s2 are two strings and when s2 is appended to the end of s1, s1 and s2 are said to be concatenated. The string s1 then contains the original string plus the string contained in s2. Similar to copying and comparing, appending s2 to s1 should be done on a character by character basis. C provides strcat() built-in function to concatenate two strings.


The prototype of strcat() is as follows:

          strcat(s1,s2);

C Program To Concatenate Two Strings:

/* Double-Click To Select Code */

#include <stdio.h>
#include <conio.h>
#include <string.h>

void main()
{ 
 char str1[20]="abc", str2[20]="def", str3[20]="ghi";
 int i,j;
 clrscr();

 printf("str1 = %s | str2 = %s \n",str1,str2);

/* Concatenation of str1 and str2 without using strcat() begins */
 
 for(i=0 ; str1[i]!='\0' ; i++);
 for(j=0 ; str2[j]!='\0' ; j++)
 {
  str1[i+j] = str2[j];
 }
 
 str1[i+j]='\0';
 printf("\nAfter concatenation str1 = %s \n",str1);

/* Concatenation of str1 and str2 without using strcat() begins */
 strcat(str1,str3);
 printf("\nstr1 = '%s' \n",str1);
 strcat(str1,"jkl");
 printf("\nstr1 = '%s' \n",str1);

 printf("\n\n *WWW.CODINGBOT.NET*");
 getch();
}

Program Explanation:

Str1, str2 and str3 are declared to be arrays of char type and all are initialized strings. The integer variables i and j are to traverse the strings. String in str2 is appended to the end of the string  in str1 by following segment of the program:

for(i=0 ; str1[i]!='\0' ; i++);
for(j=0 ; str2[j]!='\0' ; j++)
{
str1[i+j] = str2[j];
}
str1[i+j] = '\0';
The first for loop is to simply scan through the string str1. When the loop completes, the variable i points to the position of null character '\0' in str1. The second loop is to scan through the second string str2 till end of it is reached. When j takes 0, the first character in str2 is assigned to the position of null character in str1. So, the null character is str1 is overwritten by the first character in str2. Subsequently, the remaining characters in str2 are appended to str1 and lastly, the null character '\0' is assigned to the last position of str1. The string in str1, "abcdef" is then displayed.

Then two calls are mode to strcat(). In the final call, strcat(str1,str3), str3 is appended to str1. Since str3 had "ghi", sstr1 now becomes "abcdefghi" and is displayed. On the second call, strcat(str,"jkl"), the string constant "jkl" is appended to str1. The new string "abcdefghijkl" in str1 is again displayed.

Program Output:

C Code To Concatenate Two Strings


Please Comment If You Liked The Post.


Do you like this post? Please link back to this article by copying one of the codes below.

URL: HTML link code: Forum link code: