You are here

String concatenation in C

String concatenation

C program to concatenate two strings; for example, if the two input strings are "C programming" and " language" (note the space before language), then the output will be "C programming language." To concatenate the strings, we use the strcat function of "string.h", to dot it without using the library function, see another program below.

C concatenate string program

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

int main()
{
  char a[1000], b[1000];

  printf("Enter the first string\n");
  gets(a);

  printf("Enter the second string\n");
  gets(b);

  strcat(a, b);

  printf("String obtained on concatenation: %s\n", a);

  return 0;
}

Output of program:
String concatenation

Download String Concatenation program.

Concatenate strings without strcat function

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

#include <stdio.h>

void concatenate(char [], char []);
 
int main()
{
   char p[100], q[100];
 
   printf("Input a string\n");
   gets(p);
 
   printf("Input a string to concatenate\n");
   gets(q);
 
   concatenate(p, q);
 
   printf("String obtained on concatenation: \"%s\"\n", p);
 
   return 0;
}

void concatenate(char p[], char q[]) {
   int c, d;
   
   c = 0;
 
   while (p[c] != '\0') {
      c++;      
   }
 
   d = 0;
 
   while (q[d] != '\0') {
      p[c] = q[d];
      d++;
      c++;    
   }
 
   p[c] = '\0';
}

The first for loop in concatenate function is to calculate string length; you can also use strlen function if you wish.

Accessing the strings after concatenation

To concatenate two strings using strcat, we append second to the first. What if we need to access the first one further in the program?

Let's look at how to do it.

  • Since the first string is still a part of the concatenated string, we can access it if we store its length before concatenation. But, it'll increase the code size.
  • Make a copy of the first string before concatenation. This method uses some extra memory, but it's easy to implement.
  • Create a new string by copying the first into it and then concatenate it with the second.

Now the implementation:

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

int main()
{
  char firstName[1000], lastName[1000], fullName[2000];

  printf("Enter your first name\n");
  gets(firstName);

  printf("Enter your last name\n");
  gets(lastName);

  strcpy(fullName, firstName);
  strcat(fullName, " ");       // Concatenating a space
  strcat(fullName, lastName);

  printf("Hello, %s\n", fullName);

  return 0;
}

Output:

Enter your first name
John
Enter your last name
Doe
Hello, John Doe

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: \"%s\"\n", original);
       
    return 0;
}

void concatenate_string(char *original, char *add)
{
   while(*original)
      original++;
     
   while(*add)
   {
      *original = *add;
      add++;
      original++;
   }
   *original = '\0';
}