C Program to Concatenate Two Strings Using User Defined Function

This C program concatenates two string using user defined function and without using string handling function strcat().

C Source Code: String Concatenation User Defined Function


#include<stdio.h>

/* Function Prototype*/
void mystrcat(char str1[40], char str2[40]);

/* Main Function */
int main()
{
 char str1[50], str2[50];
 int i, len=0;

 printf("Enter first string:\n");
 gets(str1);
 printf("Enter second string:\n");
 gets(str2);
 mystrcat(str1, str2);
 printf("Concatenated string is: %s", str1);

 return 0;
}

void mystrcat(char str1[40], char str2[40])
{
 int i, len=0;
 /* Calculating length of first string */
 for(i=0;str1[i]!='\0';i++)
 {
  len++;
 }
 /* Concatenating second string to first string */
 for(i=0;str2[i]!='\0';i++)
 {
  str1[len+i] = str2[i];
 }
 str1[len+i]='\0';
}

Output

Enter first string:
Ram ↲
Enter second String:
Shyam ↲
Concatenated string is: RamShyam

Note: ↲ represents ENTER key is pressed.