-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-string_nconcat.c
More file actions
executable file
·68 lines (53 loc) · 918 Bytes
/
1-string_nconcat.c
File metadata and controls
executable file
·68 lines (53 loc) · 918 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
/**
* strLen - get length of string
*
* @s: pointer to string
*
* Return: lenght of string
*/
unsigned int strLen(char *s)
{
register unsigned int len;
for (len = 0; s[len] != '\0'; len++)
;
return (len);
}
/**
* string_nconcat - concat 2 strings
*
* @s1: pointer to string
* @s2: pointer to string
* @n: number of chars taken from s2
*
* Return: (NULL) if it fails, if NULL passed return (""), otherwise pointer
*/
char *string_nconcat(char *s1, char *s2, unsigned int n)
{
unsigned int i, s1Len, len;
char *mem;
if (!s1)
s1 = "";
if (!s2)
s2 = "";
s1Len = strLen(s1);
len = s1Len + n;
mem = malloc(len * sizeof(char) + 1);
if (!mem)
return (NULL);
i = 0;
while (*s1 != '\0')
{
mem[i] = *s1;
++i, ++s1;
}
while (i < len)
{
mem[i] = *s2;
++i, ++s2;
}
mem[i] = '\0';
return (mem);
}