Skip to content

Commit dc1388d

Browse files
committed
task 1
1 parent 8faedfa commit dc1388d

4 files changed

Lines changed: 53 additions & 0 deletions

File tree

0x0C-more_malloc_free/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*#
2+
*.swp

0x0C-more_malloc_free/.main.h.swp

16 KB
Binary file not shown.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "main.h"
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
/**
5+
* string_nconcat - concats strings
6+
* @s1: string one
7+
* @s2: string two
8+
* @n: n amount of bytes
9+
* Return: return a char val
10+
*/
11+
char *string_nconcat(char *s1, char *s2, unsigned int n)
12+
{
13+
unsigned int i, len1, len2;
14+
char *s;
15+
16+
if (s2 == NULL)
17+
s2 = "";
18+
if (s1 == NULL)
19+
s1 = "";
20+
21+
len1 = 0;
22+
len2 = 0;
23+
while (s2[len2] != '\0')
24+
len2++;
25+
while (s1[len1] != '\0')
26+
len1++;
27+
28+
if (n >= len2)
29+
n = len2;
30+
31+
s = malloc(sizeof(char) * n + len1 + 1);
32+
if (s == NULL)
33+
return (NULL);
34+
35+
for (i = 0; i < len1; i++)
36+
s[i] = s1[i];
37+
38+
for (i = 0; i < n; i++)
39+
s[i + len1] = s2[i];
40+
41+
s[i + len1] = '\0';
42+
43+
return (s);
44+
}

0x0C-more_malloc_free/main.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,5 +298,12 @@ void free_grid(int **grid, int height);
298298

299299
void *malloc_checked(unsigned int b);
300300

301+
/**
302+
* string_nconcat - concatenates two strings
303+
*/
304+
305+
char *string_nconcat(char *s1, char *s2, unsigned int n);
306+
307+
301308
#endif
302309

0 commit comments

Comments
 (0)