-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-str_concat.c
More file actions
executable file
·56 lines (44 loc) · 889 Bytes
/
2-str_concat.c
File metadata and controls
executable file
·56 lines (44 loc) · 889 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
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
/**
* lenStr - get length of string
*
* @s: pointer to string
*
* Return: length of string
*/
unsigned int lenStr(char *s)
{
register unsigned int i;
for (i = 0; s[i] != '\0'; i++)
;
return (i);
}
/**
* str_concat - concat 2 strings in a new string
*
* @s1: pointer to first string
* @s2: pointer to second string
*
* Return: {NULL} if 2 string are NULL or failure, otherwise {pointer}
*/
char *str_concat(char *s1, char *s2)
{
register int size1, size2, i, j;
char *nwStr;
if (!s1)
s1 = "\0";
if (!s2)
s2 = "\0";
size1 = lenStr(s1);
size2 = lenStr(s2);
nwStr = (char *) malloc(size1 * sizeof(*s1) + size2 * sizeof(*s2) + 1);
if (!nwStr)
return (NULL);
for (i = 0; i < size1; i++)
nwStr[i] = s1[i];
for (j = 0; j < size2; j++, i++)
nwStr[i] = s2[j];
return (nwStr);
}