-
Notifications
You must be signed in to change notification settings - Fork 0
/
str_functions.c
115 lines (99 loc) · 1.76 KB
/
str_functions.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "main.h"
/**
* _strcpy - copies the string pointed to by src to the buffer pointed to dest
* @dest: char dest
* @src: char src
* Return: dest, if executed properly.
*/
char *_strcpy(char *dest, char *src)
{
int i;
char a;
for (i = 0; src[i] != '\0'; i++)
{
a = src[i];
dest[i] = a;
}
dest[i] = '\0';
return (dest);
}
/**
* _strlen - returns the length of a string of the program.
* @s: pointer to String of a function
* Return: Nothing on success
*/
int _strlen(const char *s)
{
int i = 0;
while (*(s + i) != '\0')
{
i++;
}
return (i);
}
/**
* _strcat - concatenates two strings.
* @dest: string to destiny
* @src: string to source
* Return: Return a concatenate string, if executed properly
*/
char *_strcat(char *dest, char *src)
{
char *output = NULL;
unsigned int i = 0, j = 0, len1 = 0, len2 = 0;
len1 = _strlen(dest);
len2 = _strlen(src);
output = _calloc(sizeof(char), (len1 + len2 + 1));
if (output == NULL)
return (NULL);
i = 0;
j = 0;
if (dest)
{
while (i < len1)
{
output[i] = dest[i];
i++;
}
}
/*printf("output1 -> %s \n", output);*/
if (src)
{
while (i < (len1 + len2))
{
output[i] = src[j];
i++;
j++;
}
}
/*printf("output2 -> %s \n", output);*/
output[i] = '\0';
return (output);
}
/**
* _strdup - this functions copy a string.
* @str: the string to copy a function in a program
* Description: this function copy a string)?
* section header: the header of this function is hsh.
* Return: this is a void function no return, if executed properly
**/
char *_strdup(char *str)
{
int j, l;
char *s;
if (!str)
{
return (NULL);
}
l = _strlen(str) + 1;
s = _calloc(l, sizeof(char));
if (!s)
{
return (NULL);
}
for (j = 0; j < l; j++)
{
s[j] = str[j];
}
return (s);
}