forked from fvesp18/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-str_concat.c
More file actions
49 lines (44 loc) · 828 Bytes
/
2-str_concat.c
File metadata and controls
49 lines (44 loc) · 828 Bytes
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
#include "simpleshell.h"
/**
* str_concat - concatenates two strings
* @s1: string one
* @s2: string two
* Return: ptr
*/
char *str_concat(char *s1, char *s2)
{
/* declare your variables */
int len1 = 0, len2 = 0, count = 0, begs = 0;
char *cat;
/* check for empty strings */
if (s1 == NULL)
s1 = "";
if (s2 == NULL)
s2 = "";
/* find length of strings */
while (s1[len1] != '\0')
len1++;
while (s2[len2] != '\0')
len2++;
begs = len1 + len2 + 1;
/* allocate memory for concatenated string */
cat = malloc(begs * sizeof(char));
/* malloc fail safe */
if (cat == NULL)
return (NULL);
len2 = 0;
/* duplicate s1 to cat */
while (count < begs)
{
if (count < len1)
cat[count] = s1[count];
else
{
cat[count] = s2[len2];
len2++;
}
count++;
}
cat[count] = '\0';
return (cat);
}