-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrlib.c
133 lines (122 loc) · 2.23 KB
/
strlib.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "shell.h"
/**
* _strncpy - copy n bytes from src to dest
* @dest: destination string
* @src: string source
* @n: nuber of bytes to copy
*
* Return: dest
*/
char *_strncpy(char *dest, char *src, size_t n)
{
size_t i;
for (i = 0; i < n && src[i] != '\0'; i++)
dest[i] = src[i];
while (i < n)
{
dest[i] = '\0';
i++;
}
return (dest);
}
/**
* custom_concat - concate two stings with
* separator between them
* @str1: first string
* @str2: second string
* @separator: the separaator
*
* Return: concaatenation of the three
*/
char *custom_concat(char *str1, char *str2, char separator)
{
size_t len1, len2, sep_len, i;
char *result;
len1 = len2 = 0;
sep_len = 1;
while (str1[len1] != '\0')
len1++;
while (str2[len2] != '\0')
len2++;
result = (char *)malloc(len1 + len2 + sep_len + 1);
if (result == NULL)
return (NULL);
_strncpy(result, str1, len1);
result[len1] = separator;
for (i = 0; i < len2; i++)
result[len1 + sep_len + i] = str2[i];
result[len1 + sep_len + len2] = '\0';
return (result);
}
/**
* _strncmp - compare n bytes of 2 strings
* @s1: string 1
* @s2: string 2
* @n: number of bytes to compare
*
* Return: 0 or -15
*/
int _strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
for (i = 0; s1[i] && s2[i] && i < n; i++)
{
if (s1[i] > s2[i])
return (s1[i] - s2[i]);
else if (s1[i] < s2[i])
return (s1[i] - s2[i]);
}
if (i == n)
return (0);
else
return (-15);
}
/**
* _strdup - dupmicate string
* @s: string to dup
*
* Return: the resulting string
*/
char *_strdup(const char *s)
{
int i, len = 0;
char *new_str;
if (s == NULL)
return (NULL);
while (s[len] != '\0')
len++;
new_str = (char *)malloc((len + 1) * sizeof(char));
if (new_str == NULL)
return (NULL);
for (i = 0; i <= len; i++)
new_str[i] = s[i];
return (new_str);
}
/**
* intToString - Convert int in string
* @num: int to convert
*
* Return: the string resulting from num
*/
char *intToString(int num)
{
int i, len = 1;
int temp = num;
char *str;
while (temp /= 10)
{
len++;
}
str = (char *)malloc((len + 1) * sizeof(char));
if (str == NULL)
return (NULL);
i = len - 1;
str[len] = '\0';
while (num != 0)
{
str[i] = '0' + (num % 10);
num /= 10;
i--;
}
return (str);
}