Concatenate two strings without using strcat() function

Let's write a c program to concatenate two strings without using strcat() function.

Example

Input

com

puter

Output

computer

Input

in

dia

Output

india




Concatenate two strings program with logic explanation

Example

#include<stdio.h>

int main()
{
    char str1[100],str2[100],str[200];
    int length = 0,i;
    
    scanf("%s%s",str1,str2);
    
    /*  Let's assume 
     *  first string as "abc",
     *  second string as "def"
     */
    
    /* fill the first string str1
     * str[0] = 'a' length = 1
     * str[1] = 'b' length = 2
     * str[2] = 'c' length = 3
     */
    for(i = 0; str1[i] != '\0'; i++)
          str[length++] = str1[i];
          
    /* fill the second string str2
     * str[3] = 'd' length = 4
     * str[4] = 'e' length = 5
     * str[5] = 'f' length = 6
     */
    for(i = 0; str2[i] != '\0'; i++)
          str[length++] = str2[i];
    
    /*append null char '\0' at the end of string
     *str[6] = '\0';
     */
      
    str[length] = '\0';
    
    /*
     * str[0] = 'a'
     * str[1] = 'b'
     * str[2] = 'c'
     * str[3] = 'd'
     * str[4] = 'e'
     * str[5] = 'f'
     * str[6] = '\0'
     */
     
    printf("Final String = %s",str);
    
    return 0;
}


Topics You Might Like