-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconcat_strings.c
46 lines (37 loc) · 1.01 KB
/
concat_strings.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
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include "header.h"
#include <dirent.h>
#include "libshell/libshell.h"
#define BUFFER_SIZE 100
/*
* concat_strings() - Take two strings and concatenate them.
* @s1: The first string.
* @s2: The second string.
*
* Return: The pointer with the concatentated string.
*/
char *concat_strings(char *s1, char *s2)
{
int i, j;
char *p;
p = malloc( sizeof(char) * ( str_len(s1) + str_len(s2) ) + 1 ); /* allocate memory */
if (p == NULL) { /* memory allocation check */
perror("malloc");
return "Not enough memory allocated.";
}
j = 0;
for (i = 0; s1[i] != '\0'; ++i) { /* here we copy the first string onto p */
p[j] = s1[i];
++j;
}
for (i = 0; s2[i] != '\0'; ++i) { /* here we append the 2nd string onto p */
p[j] = s2[i];
++j;
}
p[j] = '\0'; /* append null character at the end */
return p;
}