C Program to Concatenate Two Strings


This is a C program to read two strings & concatenate the strings.

Problem Description

This program takes two strings as input and concatenate them.

Problem Solution

1. Take two strings as input and store them in two different arrays.
2. Find the position of the last element of the first array and from that position keep on adding the elements of the second array.
3. Exit.

Program/Source Code

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

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

1. Take two strings as input and store them in the arrays string1 and string2 respectively.
2. Using for loop find the position of the last element of the array string1[]. Store that position in the variable pos.
3. Using another for loop add the elements of the array string2[] into the array string1[] starting from the obtained position.
4. Print the array string1[] as output.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
Enter the first string : San
Enter the second string: foundry
First string  = San
Second string = foundry
Concatenated string = Sanfoundry

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

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.