38 lines
601 B
C
38 lines
601 B
C
/*
|
||
* concat.c
|
||
*
|
||
* Created on: 6 июл. 2022 г.
|
||
* Author: alexander
|
||
*/
|
||
|
||
#include "concat.h"
|
||
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
#include <stdio.h>
|
||
|
||
char* concat(char *s1, char *s2)
|
||
{
|
||
size_t len1 = s1 ? strlen(s1) : 0;
|
||
size_t len2 = s2 ? strlen(s2) : 0;
|
||
|
||
char *result = (char *)malloc(sizeof(char) * (len1 + len2 + 1));
|
||
|
||
if (!result)
|
||
{
|
||
fprintf(stderr, "malloc() failed: insufficient memory!\n");
|
||
return NULL;
|
||
}
|
||
|
||
if (s1)
|
||
{
|
||
strcpy(result, s1);
|
||
}
|
||
if (s2)
|
||
{
|
||
strcpy(result + len1, s2);
|
||
}
|
||
|
||
return result;
|
||
}
|