-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-string_nconcat.c
54 lines (46 loc) · 965 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
49
50
51
52
53
54
#include "holberton.h"
/**
* _strlen - measures the length of a string.
* Descritpion: measures the string s.
* @s: string to be messured/
* Return: returns length of s.
*/
unsigned int _strlen(char *s)
{
unsigned int count;
count = 0;
while (s[count] != '\0')
{
count++;
}
return (count);
}
/**
* string_nconcat - cats two strings by amount n.
* @s1: string 1.
* @s2: string 2.
* @n: amount to take.
* Return: returns pointer to new string, NULL on fail.
*/
char *string_nconcat(char *s1, char *s2, unsigned int n)
{
char *temp;
unsigned int h, i, size;
if (s1 == NULL)
s1 = "";
if (s2 == NULL)
s2 = "";
if (n > _strlen(s2))
size = _strlen(s1) + _strlen(s2);
else
size = _strlen(s1) + n;
temp = malloc((size + 1) * sizeof(char));
if (temp == NULL)
return (NULL);
for (h = 0; s1[h] != '\0'; h++)
temp[h] = s1[h];
for (i = 0; h < size && s2[i] != '\0'; i++, h++)
temp[h] = s2[i];
temp[h] = '\0';
return (temp);
}