/
_strdup.c
36 lines (30 loc) · 828 Bytes
/
_strdup.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
#include "shell.h"
/**
* _strdup - Duplicate a string.
* @s: The string to be duplicated.
*
* Return: Pointer to the duplicated string.
*/
char *_strdup(const char *s)
{
char *str;
size_t i = 0, j = 0;
/* Check if the input string is NULL */
if (s == NULL)
return (NULL);
/* Loop through the input string to find its length */
for (i = 0; s[i] != '\0'; i++)
;
/* Allocate memory for the duplicated string */
str = (char *)malloc((i + 1) * sizeof(char));
/* Check if memory allocation was successful */
if (str == NULL)
return (NULL);
/* Copy the characters from the input string to the duplicated string */
for (j = 0; j < i; j++)
str[j] = s[j];
/* Add the null terminator at the end of the duplicated string */
str[i] = '\0';
/* Return the pointer to the duplicated string */
return (str);
}