-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-string_nconcat.c
48 lines (43 loc) · 892 Bytes
/
1-string_nconcat.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
#include <stdio.h>
#include <stdlib.h>
/**
* string_nconcat - function to concatnate strings with n bytes
* @s1: destination for concatnation
* @s2: source of string
* @n: int type for size of byte
* Return: pointer to new memory allocated
*/
char *string_nconcat(char *s1, char *s2, unsigned int n)
{
int count, count1;
int sign = n;
char *ptr;
int len1, len2;
if (s1 == NULL)
s1 = "";
if (s2 == NULL)
s2 = "";
for (len1 = 0; s1[len1] != '\0'; len1++)
;
for (len2 = 0; s2[len2] != '\0'; len2++)
;
if (sign >= len2)
{
sign = len2;
ptr = malloc(sizeof(char) * (len1 + len2 + 1));
}
else
ptr = malloc(sizeof(char) * (len1 + n + 1));
if (ptr == NULL)
return (NULL);
for (count = 0; count < len1; count++)
{
ptr[count] = s1[count];
}
for (count1 = 0; count1 < sign; count1++)
{
ptr[count++] = s2[count1];
}
ptr[count++] = '\0';
return (ptr);
}