-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.c
79 lines (65 loc) · 1.29 KB
/
utilities.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
/**
* _strcat - concatenates two strings
*
* @dest: destination string
*
* @src: source string
*
* Return: a pointer to the start of the destination string
*/
char *_strcat(char *dest, char *src)
{
char *str = dest;
int i, j, ls, ld;
ls = 0;
while (str[ls] != '\0')
ls++;
ld = 0;
while (str[ld] != '\0')
ld++;
for (i = ld, j = 0; i <= ld + ls; i++, j++)
dest[i] = src[j];
return (str);
}
/**
* itoa - converts an integer to a string
*
* @n: number to convert
*
* @buffer: buffer to store the converted number
*
* Return: the converted number is string
*/
char *itoa(unsigned int n, char *buffer)
{
if (n / 10 != 0)
buffer = itoa(n / 10, buffer);
*buffer = n % 10 + '0';
buffer++;
return (buffer);
}
/**
* startwith - checks if a string starts with a given substring
*
* @str: string in which to search for a given substring
*
* @substr: substring to search in the string
*
* Return: 1 if the substring is found at the start of the string
* otherwise return 0
*
* if `str` or `substr` is NULL or empty, return 0
*/
int startwith(char *str, char *substr)
{
int i = 0, j = 0;
if (!str || !substr || !*str || !*substr)
return (0);
while (substr[j] != '\0')
j++;
while (str[i] == substr[i] && substr[i] != '\0')
i++;
if (j != i)
return (0);
return (1);
}