Home

A program to concatenate two strings and print the resultant string without using library function

C Programming Language 068

The following code shows how to write a program to concatenate (JOIN) two strings and print the resultant string without using library function.

Program

#include <stdio.h>

void main()
{
    char str1[30], str2[30], str3[30];
    int i, j = 0;

    printf("Enter two strings:\n");
    gets(str1);
    gets(str2);

    for (i = 0; str1[i] != '\0'; i++)
    {
        str3[j] = str1[i];
        j++;
    }

    for (i = 0; str2[i] != '\0'; i++)
    {
        str3[j] = str2[i];
        j++;
    }
    str3[j] = '\0';

    printf("str1 = %s \nstr2 = %s \nstr3 = %s", str1, str2, str3);

    printf("\n");
}

Result

Enter two strings:
hello
world
str1 = hello 
str2 = world 
str3 = helloworld


Last Updated on

Next Post: Rules vs Guidelines →

Comments