Thursday, 21 August 2014

Filled Under: ,

C program to Concatenate Two Strings

Share


Here a simple C program to Concatenate Two Strings is given below.Which takes two string as input and after execution it will give concatenated string as output on output screen.You can concatenate two strings easily using standard library function strcat( ) but, this program concatenates two strings manually without using strcat( ) function.

 C program to Concatenate Two Strings

#include <stdio.h>
int main()
{
    char s1[100], s2[100], i, j;
    printf("Enter first string: ");
    scanf("%s",s1);
    printf("Enter second string: ");
    scanf("%s",s2);
    for(i=0; s1[i]!=''; ++i);  /* i contains length of string s1. */
    for(j=0; s2[j]!=''; ++j, ++i)
    {
        s1[i]=s2[j];
    }
    s1[i]='';
    printf("After concatenation: %s",s1);
    return 0;
}
Output
Enter first string: geek
Enter second string: coderz
After concatenation: geekcoderz