-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path2-str_concat.c
39 lines (30 loc) · 838 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
39
#include "main.h"
#include <stdlib.h>
/**
* str_concat - Concatenates two strings.
* @s1: The string to be concatenated upon.
* @s2: The string to be concatenated to s1.
*
* Return: If concatenation fails - NULL.
* Otherwise - a pointer the newly-allocated space in memory
* containing the concatenated strings.
*/
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);
}