-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy path1-string_nconcat.c
More file actions
49 lines (37 loc) · 755 Bytes
/
1-string_nconcat.c
File metadata and controls
49 lines (37 loc) · 755 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 "main.h"
#include <stdlib.h>
/**
* string_nconcat - concatenates two strings.
* @s1: first string.
* @s2: second string.
* @n: amount of bytes.
*
* Return: pointer to the allocated memory.
* if malloc fails, status value is equal to 98.
*/
char *string_nconcat(char *s1, char *s2, unsigned int n)
{
char *sout;
unsigned int ls1, ls2, lsout, i;
if (s1 == NULL)
s1 = "";
if (s2 == NULL)
s2 = "";
for (ls1 = 0; s1[ls1] != '\0'; ls1++)
;
for (ls2 = 0; s2[ls2] != '\0'; ls2++)
;
if (n > ls2)
n = ls2;
lsout = ls1 + n;
sout = malloc(lsout + 1);
if (sout == NULL)
return (NULL);
for (i = 0; i < lsout; i++)
if (i < ls1)
sout[i] = s1[i];
else
sout[i] = s2[i - ls1];
sout[i] = '\0';
return (sout);
}