C Program to Concatenate Two Strings

In this C program, we will join two given strings using strcat function and by using pointers. We first take two string as input from user using gets function and store it in two character array. Now, we have to append first string at the end of second string.

We can either use strcat function of string.h header file to concatenate strings or write our own function to append strings using pointers. Second string must be large enough to contain the concatenated resulting string. For example, if first string is "Tech" and second string is "CrashCourse" then on concatenating these two strings we get "TechCrashCourse".


C program to concatenate two strings using strcat function

To use strcat function, we must include string.h header file in our program. Here is the declaration for strcat function.


char *strcat(char *destination, const char *source);

The strcat() function concatenates a source string to destination string and adds a null character at the end of destination. Source string is not modified in this operation. It returns concatenated destination string. Length of destination string must be more than sum of original length of both strings.

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

int main(){
    char destination[100], source[100];
    
    printf("Enter first string \n");
    gets(destination);
    printf("Enter second string \n");
    gets(source);
 
    strcat(destination, source);
    
    printf("Concatenated String: %s \n",
       destination);
    
    return 0;
}
Output
Enter first string
Tech
Enter second string
CrashCourse
Concatenated String: TechCrashCourse

C program to concatenate two strings using pointers

In this program, we are using a user defined function 'concatenateString' to concatenate two strings. It takes source and destination string pointers as parameters and does input validation(neither source nor destination pointer should be NULL). It calculate length of destination string using strlen and then appends characters of source string into destination string by overwriting null character of destination string. At last it adds null character in destination string and returns it.

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

int getStringlength(char *inputString);
char* concatenateString(char *firstString, char *secondString);

int main(){
    char destination[100], source[100];
    
    printf("Enter first string \n");
    gets(destination);
    printf("Enter second string \n");
    gets(source);
    
    concatenateString(destination, source);
    
    printf("Concatenated String: %s \n", destination);
    
    return 0;
}


char* concatenateString(char *destination, char *source){
    if(NULL == destination || NULL == source){
        return NULL;
    }
    int index = 0, length = strlen(destination);
    while(source[index] != '\0'){
        destination[length] = source[index];
        length++;
        index++;
    }
    destination[length] = '\0';
    return destination;
}
Output
Enter first string
Hello
Enter second string
World
Concatenated String: HelloWorld

Related Topics
C program to swap two strings
C program to compare two strings
C program to reverse a string
C Program to find frequency of characters in a string
C program to convert uppercase string to lowercase
C program to find a substring from a given string
C program to sort characters of a string
List of all C programs