2022-07-06 17:51:51 +00:00
|
|
|
|
/*
|
|
|
|
|
* 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;
|
|
|
|
|
|
2022-07-08 16:45:45 +00:00
|
|
|
|
char *result = (char *)malloc(sizeof(char) * (len1 + len2 + 1));
|
2022-07-06 17:51:51 +00:00
|
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "malloc() failed: insufficient memory!\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (s1)
|
|
|
|
|
{
|
2022-07-19 10:13:57 +00:00
|
|
|
|
strncpy(result, s1, (len1 + 1));
|
2022-07-06 17:51:51 +00:00
|
|
|
|
}
|
|
|
|
|
if (s2)
|
|
|
|
|
{
|
2022-07-19 10:13:57 +00:00
|
|
|
|
strncpy(result + len1, s2, (len2 + 1));
|
2022-07-06 17:51:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|