-
Notifications
You must be signed in to change notification settings - Fork 11
/
2-str_concat.c
38 lines (28 loc) · 691 Bytes
/
2-str_concat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "main.h"
/**
* str_concat - a function that concatenates two strings.
*@s1:First string
*@s2:Second string
*
*Return: NULL in case of failure , but pointer to new string in
*case of success
*/
char *str_concat(char *s1, char *s2)
{
char *concat_str;
int index, concat_index = 0, len = 0;
if (s1 == NULL)
s1 = "";
if (s2 == NULL)
s2 = "";
for (index = 0; s1[index] || s2[index]; index++)
len++;
concat_str = malloc(sizeof(char) * len);
if (concat_str == NULL)
return (NULL);
for (index = 0; s1[index]; index++)
concat_str[concat_index++] = s1[index];
for (index = 0; s2[index]; index++)
concat_str[concat_index++] = s2[index];
return (concat_str);
}