-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf_strings.c
More file actions
109 lines (96 loc) · 1.77 KB
/
f_strings.c
File metadata and controls
109 lines (96 loc) · 1.77 KB
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
#include "shell.h"
/**
* _strlen - returns the length of a string
* @s: array
* Return: length
*/
int _strlen(const char *s)
{
int i = 0;
if (s == NULL)
return (0);
while (s[i])
i++;
return (i);
}
/**
* _strdup - copy a strings with a malloc
* @s1: array
* Return: pointer to the copy
*/
char *_strdup(const char *s1)
{
char *s2;
size_t i;
i = 0;
s2 = (char *)malloc(sizeof(char) * _strlen(s1) + 1);
if (!s2)
return (NULL);
while (s1[i])
{
s2[i] = s1[i];
i++;
}
s2[i] = '\0';
return (s2);
}
/**
* _strncmp - compare if is equal 2 strings, first n chars
* @s1: string1
* @s2: string2
* @n: n first characters
* Return: 0 success; else pointer
*/
int _strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
i = 0;
while (i < n && s1 && s2 && s1[i] && s2[i] &&
(unsigned char)s1[i] == (unsigned char)s2[i])
i++;
if (i == n)
return (0);
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}
/**
* _strcmp - compare if is equal 2 strings
* @s1: string1
* @s2: string2
* Return: 0 success; else pointer
*/
int _strcmp(const char *s1, const char *s2)
{
return (_strncmp(s1, s2, (size_t)-1));
}
/**
* num_to_str - Convert a number to string format
* @num: Number to be converted
*
* Return: Pointer to string representation of "num"
*/
char *num_to_str(int num)
{
int num_rev = 0;
int i, digits = 0;
char *num_str = NULL;
if (num == 0)
digits = 1;
else
while (num > 0)
{
digits++;
num_rev *= 10;
num_rev += num % 10;
num /= 10;
}
num_str = malloc(sizeof(char) * (digits + 1));
if (num_str == NULL)
dispatch_error("Error: Coudn't allocate memory for number conversion");
for (i = 0; i < digits; i++)
{
num_str[i] = (num_rev % 10) + '0';
num_rev /= 10;
}
num_str[i] = '\0';
return (num_str);
}