Table of Contents

    strcat() Function in C: Concatenating Strings

    strcat() Function in C: Concatenating Strings

    strcat( ) function in C language concatenates two given strings. It concatenates source string at the end of destination string. Syntax for strcat( ) function is given below.

    Syntax

    char * strcat ( char * destination, const char * source );
    Method Description
    strcat ( strng2, strng1 ); strng1 is concatenated at the end of strng2.
    strcat ( strng1, strng2 );  strng2 is concatenated at the end of strng1.

    Parameters

    strng1- this is a string(Like: "hi") strng2- this is a string(Like: "hello")

    Returns

    This function concatenates the source string at the end of the target string. For example, "Hi" and "Hello" on concatenation would result into a string "HiHello".

    Program 1

    
    #include<stdio.h>
    #include<string.h>
    
    int main( )
    {
       char strng1[ ] = "Hello " ;
       char strng2[ ]= "Hi" ;
    
       printf ( "\nSource string = %s", strng1 ) ;
       printf ( "\nTarget string = %s", strng2 ) ;
    
       strcat(strng1, strng2) ;
    
       printf("\nTarget string after strcat() = %s \n", strng1) ;
    }
    

    Output

    Source string = Hello
    Target string = Hi
    Target string after strcat() = Hello Hi
    Press any key to continue . . .

    Program 2

    
    #include<stdio.h>
    #include<string.h>
    void main( )
    {
    char source[ ] = "Don!" ;
    char target[30] = "I am " ;
    strcat ( target, source ) ;
    printf ( "\nsource string = %s", source ) ;
    printf ( "\ntarget string = %s \n", target ) ;
    }
    

    Output

    source string = Don!
    target string = I am Don!
    Press any key to continue . . .

    Note that the target string has been made big enough to hold the final string.