-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-str_concat.c
73 lines (59 loc) · 844 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "main.h"
/**
* _strlen - returns the length of a string
* @str: string whose length will be returned
*
* Return: length of string
*/
int _strlen(char *str)
{
int len;
len = 0;
while (*str != '\0')
{
str++;
len++;
}
return (len);
}
/**
* str_concat - concatenates two strings and allocates new memory
* @s1: The first string passed
* @s2: The second string passed
*
* Return: length of string
*/
char *str_concat(char *s1, char *s2)
{
char *s;
int i, j, size;
i = 0;
j = 0;
if (s1 == NULL)
{
s1 = "";
}
if (s2 == NULL)
{
s2 = "";
}
size = _strlen(s1) + _strlen(s2) + 1;
s = malloc(sizeof(char) * size);
if (s == NULL)
{
return (NULL);
}
while (s1[i] != '\0')
{
s[i] = s1[i];
i++;
}
while (s2[j] != '\0')
{
s[i] = s2[j];
i++;
j++;
}
s[size] = '\0';
return (s);
}