C program to concatenate strings

This program concatenates strings, for example if the first string is "c " and second string is "program" then on concatenating these two strings we get the string "c program". To concatenate two strings we use strcat function of string.h, to concatenate without using library function see another code below which uses pointers.

C programming code

#include <stdio.h>
#include <string.h>
 
int main()
{
   char a[100], b[100];
 
   printf("Enter the first string\n");
   gets(a);
 
   printf("Enter the second string\n");
   gets(b);
 
   strcat(a,b);
 
   printf("String obtained on concatenation is %s\n",a);
 
   return 0;
}

Download String Concatenation program.

Output of program:
String concatenation

Concatenate strings without strcat function

C program to concatenate strings without using library function strcat of header file string.h.

#include <stdio.h>
 
int main()
{
   char p[100], q[100];
   int c, d;
 
   printf("Input a string\n");
   gets(p);
 
   printf("Input a string to concatenate\n");
   gets(q);
 
   c = 0;
 
   while (p[c] != '\0') {
      c++;  	
   }
 
   d = 0;
 
   while (q[d] != '\0') {
      p[c] = q[d];
      d++;
      c++;	
   }
 
   p[c] = '\0';
 
   printf("String obtained on concatenation is \"%s\"\n", p);
 
   return 0;
}

String concatenation using pointers

#include <stdio.h>
 
void concatenate_string(char*, char*);
 
int main()
{
    char original[100], add[100];
 
    printf("Enter source string\n");
    gets(original);
 
    printf("Enter string to concatenate\n");
    gets(add);
 
    concatenate_string(original, add);
 
    printf("String after concatenation is \"%s\"\n", original);
 
    return 0;
}
 
void concatenate_string(char *original, char *add)
{
   while(*original)
      original++;
 
   while(*add)
   {
      *original = *add;
      add++;
      original++;
   }
   *original = '\0';
}
randomness