Concatenating Character Strings with function - C Function

C examples for Function:Utility Function

Description

Concatenating Character Strings with function

Demo Code

#include <stdio.h>
void concat (char result[], const char str1[], const char str2[]);
int main (void){

    const char s1[] = { "Test " };
    const char s2[] = { "works." };
    char s3[20];/*from  w  w  w.j a  v  a2  s . c  o  m*/

    concat (s3, s1, s2);

    printf ("%s\n", s3);

    return 0;
}

//Function to concatenate two character strings.

void concat (char result[], const char str1[], const char str2[]){
    int i, j;

    // copy str1 to result
    for ( i = 0; str1[i] != '\0'; ++i)
        result[i] = str1[i];

    // copy str2 to result
    for ( j = 0; str2[j] != '\0'; ++j)
        result[i + j] = str2[j];

    // add a null character to the concatenated string to terminate it
    result[i + j] = '\0';
}

Related Tutorials