-
Notifications
You must be signed in to change notification settings - Fork 0
/
com_str1.c
92 lines (74 loc) · 1.64 KB
/
com_str1.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "main.h"
/**
* _strcpy - copies string pointed to by src to another variable
* @dest: pointer var to a buffer where to copy the string
* @src: pointer var to the source of the string to be copied
* Return: the pointer to dest i.e. the address
*/
char *_strcpy(char *dest, char *src)
{
int i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}
/**
* _strcat - concatenates two strings
* @dest: pointer to the first char of destination string
* @src: pointer to the first char of source string
* Return: pointer to the concatenated string
*/
char *_strcat(char *dest, char *src)
{
int i, j;
char *result = NULL;
int size_dest = _strlen(dest);
int size_src = _strlen(src);
result = malloc((size_dest + size_src + 1) * sizeof(char));
if (result == NULL)
return (NULL);
for (i = 0; i <= size_dest - 1; i++)
result[i] = dest[i];
for (i = 0; i <= size_src - 1; i++)
{
j = size_dest + i;
result[j] = src[i];
}
result[j + 1] = '\0';
return (result);
}
/**
* _strlen - returns the length of a function using recursion
* @s: pointer to the string
* Return: the length of the string as integer
*/
int _strlen(char *s)
{
int len = 0;
while (s[len] != '\0')
len++;
return (len);
}
/**
* _strdup - duplicates a string
* @str: pointer to the string to duplicate
* Return: pointer to the copy of the string
*/
char *_strdup(char *str)
{
char *str_cpy;
int len, i;
if (str == NULL)
return (NULL);
len = _strlen(str) + 1;
str_cpy = malloc(sizeof(*str_cpy) * len);
if (str_cpy == NULL)
return (NULL);
for (i = 0; i < len; i++)
str_cpy[i] = str[i];
return (str_cpy);
}