Concatenate Two Strings in C


Write a C Program to concatenates two strings.

Click Here to View the Solution!
#include <stdio.h>
int main() {
    char s1[100] = "Code Of Code is", s2[] = " a great website";
    int length, j;
    length = 0;
    while (s1[length] != '\0') {
    for (j = 0; s2[j] != '\0'; ++j, ++length) {
        s1[length] = s2[j];
    s1[length] = '\0';
    printf("After concatenation: ");
    return 0;
Click Here to View the Output!
After concatenation: Code Of Code is a great website
Click Here to View the Explanation!
  • This program is used to concatenate to string arrays containing strings without using the strcat() function.
  • In main(), two string arrays s1 and s2 are initialized each with a string stored in it.
  • A variable length is initialized to 0. A while loop is used which continues incrementing the variable length until a null character is observed in s1. Hence, the length of s1 is stored in the variable length.
  • The string s2 is then concatenated to the string s1 by iterating over the characters of s2 and each time incrementing the length.
  • The string s1 is then terminated assigning it the null character.
  • After the two strings are concatenated, the output is out into the string s1.
  • It is to be noted, that the length of s1 should be sufficient so that it can hold the string s2 after concatenation.
%d bloggers like this: