C Program to Concatenate Two Strings

«
»

String concatenation in C is the technique of joining two strings together to create a single string. If there are two strings, the second string is appended to the end of the first string. “+” operator is used to concatenate the strings.

For Example, first string = String; second string = Programs

String + Programs = String Programs

Problem Description

Write a C program that takes two strings as input and concatenates them.

Problem Solution

1. Take the two strings as input.
2. Concatenate the second string to the first string.
3. Print the concatenated string.

Let’s discuss different ways to concatenate the strings in C language.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Method 1: String Concatenation in C using Loops (Naive Approach)

In this approach, we will concatenate the second string to the first string without using any user or library functions.

Methods used: (Inbuilt)

  • memset(char*, int, int) – This function sets the first n characters of the string to the value of char*.
  • printf(char*, …) – This function prints the string to the standard output using specified format.

Approach to Concatenate Two Strings in C using Loops
1. Take the two strings as input.
2. Concatenate the second string to the first string after making sure that the first string has enough space to store the second string.
3. Print the concatenated string.

Example

Input:
Enter the first string: “Sanfoundry”
Enter the second string: “Programming”

Output:
Concatenated string = SanfoundryProgramming

Program/Source Code

Here is source code of the C program to read two strings & concatenate the strings using loops. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* C program to read two strings and concatenate them, without using 
  2.  * library functions. Display the concatenated string. 
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. int main(void)
  8. {
  9.     char string1[20], string2[20];
  10.     int i, j; /*  Initialize the string to NULL values */
  11.     memset(string1, 0, 20);
  12.     memset(string2, 0, 20);
  13.     printf("Enter the first string : ");
  14.     scanf("%s", string1);
  15.     printf("Enter the second string: ");
  16.     scanf("%s", string2);
  17.     printf("First string  = %s\n", string1);
  18.  
  19.     /*  Concat the second string to the end of the first string */
  20.     printf("Second string = %s\n", string2);
  21.     for (i = 0; string1[i] != '\0'; i++)
  22.     { 
  23.      /*  null statement: simply traversing the string1 */ ;
  24.     }
  25.     for (j = 0; string2[j] != '\0'; i++)
  26.     {
  27.         string1[i] = string2[j++];
  28.     } /*  set the last character of string1 to NULL */
  29.     string1[i] = '\0';
  30.     printf("Concatenated string = %s\n", string1);
  31. }
Program Explanation

1. The program asks the user to enter the two strings.
2. Making sure that the first string has enough space to store the second string and is null terminated.
3. Then a loop passes all the stored characters in string1 and reaches the first null character.
4. Then a loop passes all the stored characters in string2 and concatenates them to the end of string1. 5. Finally, the concatenated string is printed.

advertisement

Time complexity: O(n)
Time complexity of the above algorithm is O(n).

Space Complexity: O(1)
Space complexity of the above algorithm is O(1).

Run Time Testcases

Testcase 1: In this case, we are entering the strings to concatenate are “Sanfoundry” and “Programming” as input.

Enter the first string : Sanfoundry
Enter the second string: Programming
First string  = Sanfoundry
Second string = Programming
Concatenated string = SanfoundryProgramming

Testcase 2: In this case, we are entering the strings “India” and “is my country” as input.

advertisement
Enter the first string : India
Enter the second string: is my country
First string  = India
Second string = is my country
Concatenated string = Indiais my country

Method 2: Concatenate Two Strings in C using Function

In this approach, we’ll use a separate function to concatenate the second string to the first string.

Methods used:
char *str_concat(char*, char*) – This function concatenates the second string to the first string.

Inbuilt functions used:

  • malloc(size_t) – This function allocates memory for the string.
  • scanf(char*, …) – This function reads the string from the standard input.
  • sizeof(char) – This function returns the size of the character.

Approach to Concatenate Two Strings using Function
1. Take the two strings as input.
2. Call the function str_concat to concatenate the second string to the first string.
3. Print the concatenated string.

Example

Input:
Enter first string: “Programming”
Enter second string: “is fun”

Output:
Programmingis fun

Program/Source Code

Here is source code of the C program to concatenate two strings using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* C program to concatenate two strings using function */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. char *str_concat(char *str1, char *str2)
  7. {
  8.     char *str3;
  9.     int i, j, k;
  10.     i = j = k = 0;
  11.     while (str1[i] != '\0')
  12.         i++;
  13.     while (str2[j] != '\0')
  14.         j++;
  15.     str3 = (char *)malloc((long unsigned) (i + j + 1) * sizeof(char));
  16.     while (k < i)
  17.         str3[k] = str1[k];
  18.         k++;
  19.     while (k < (i + j))
  20.         str3[k] = str2[k - i];
  21.         k++;
  22.     str3[k] = '\0';
  23.     return str3;
  24. }
  25.  
  26. int main(void)
  27. {
  28.     printf("Enter first string: ");
  29.     char str1[100];
  30.     scanf("%s", str1);
  31.     printf("Enter second string: ");
  32.     char str2[100];
  33.     scanf("%s", str2);
  34.     char *str3;
  35.     str3 = str_concat(str1, str2);
  36.     printf("%s\n", str3);
  37.     return 0;
  38. }
Program Explanation

The program asks the user to enter the two strings. Then the function str_concat is called to concatenate the second string to the first string. Finally, the concatenated string is printed.

In the str_concat function, we first initialize the variables i, j and k to 0. Then we traverse the first string and store the characters in str3 until the first null character is encountered. Then we traverse the second string and store the characters in str3 after the first null character.

Time complexity: O(n)
The time complexity of the above algorithm is O(n).

Space Complexity: O(n)
The space complexity of the above algorithm is O(n).

Run Time Testcases

Here, we are entering the strings “Programming” and “is fun” as input.

Enter first string: Programming
Enter second string: is fun
Programmingis fun

Method 3: Concatenate Two Strings in C using Advanced Approach

In this approach, we’ll use a separate function to concatenate the second string to the first string and make use of library functions.

Methods used:
char *str_concat(char*, char*) – This function concatenates the second string to the first string and stores the concatenated string in a new string.

Inbuilt functions used:

  • strcat(char*, char*) – This function concatenates the second string to the first string.
  • strlen(char*) – This function returns the length of the string.
  • malloc(size_t) – This function allocates memory for the string.
  • scanf(char*, …) – This function reads the string from the standard input.
  • strcpy(char*, char*) – This function copies the string from the second string to the first string.

Approach:
1. Take the two strings as input.
2. Call the function str_concat to concatenate the second string to the first string.
3. Allocate memory for the third string as the length of the first string plus the length of the second string.
4. Copy the first string to the third string using strcpy function.
5. Copy the second string to the third string using strcat function.
6. Print the concatenated string.

Examples:

Input: “San”, “foundry” Output: “Sanfoundry”

Input: “Lets”, “code” Output: “Letscode”

Program/Source Code

Here is source code of the C program to concatenate two strings using inbuilt function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* C program to concatenate two strings without malloc */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. char *str_concat(char *str1, char *str2)
  8. {
  9.     char *str3;
  10.     str3 = (char *)malloc((strlen(str1) + strlen(str2) + 1) * sizeof(char));
  11.     strcpy(str3, str1);
  12.     strcat(str3, str2);
  13.     return str3;
  14. }
  15.  
  16. int main(void)
  17. {
  18.     printf("Enter first string: ");
  19.     char str1[100];
  20.     scanf("%s", str1);
  21.     printf("Enter second string: ");
  22.     char str2[100];
  23.     scanf("%s", str2);
  24.     char *str3;
  25.     str3 = str_concat(str1, str2);
  26.     printf("Concatenated string = %s\n", str3);
  27.     return 0;
  28. }
Program Explanation

In this program, we first ask the user to enter the two strings. Then the function str_concat is called to concatenate the second string to the first string and stores the concatenated string in a new string. Finally, the concatenated string is printed.

In the str_concat function, we first initialize the variable str3 to point to the memory allocated for the third string. Then we copy the first string to the third string using strcpy function. Then we copy the second string to the third string using strcat function.

Time complexity: O(n)
The time complexity of the above algorithm is O(n).

Space Complexity: O(n)
The space complexity of the above algorithm is O(n).

Run Time Testcases

Testcase 1: In this case, we are entering the strings “Lets” and “code” as input.

Enter first string : Lets
Enter second string: code
Concatenated string = Letscode

Testcase 2: In this case, we are entering the strings “San” and “foundry” as input.

Enter first string : San
Enter second string: foundry
Concatenated string = Sanfoundry

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.