-
Notifications
You must be signed in to change notification settings - Fork 1
/
strings.c
118 lines (101 loc) · 1.96 KB
/
strings.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
#include "main.h"
/**
* _strcat - append two strings.
* @dest: string destination.
* @src: string input.
* Return: destination.
*/
char *_strcat(char *dest, char *src)
{
char *p = dest;
while (*p != '\0')
p++;
while (*src != '\0')
*p++ = *src++;
*p = '\0';
return (dest);
}
/**
* _strcmp - Compare two strings.
* @s1: first string to compare.
* @s2: seconde string to compare.
* Return: 0 if *s1 and *s2 are equal,
* -1 if *s1 is less than *s2, 1 if *s1 is greater than *s2.
*/
int _strcmp(char *s1, char *s2)
{
int counter = 0, comparison = 0;
while (s1[counter] && s2[counter])
{
if (s1[counter] != s2[counter])
{
comparison = s1[counter] - s2[counter];
break;
}
counter++;
}
return (comparison);
}
/**
* _strstr - Locate a substring.
* @environ: string in which to look at.
* @path: substring to find.
* Return: 1 on success, 0 on failure.
*/
int _strstr(char *environ, char *path)
{
char *env = environ;
char *_path = path;
environ = env;
while (*_path != '\0' && *env == *_path)
{
env++, _path++;
if (*_path == '\0')
return (1);
}
return (0);
}
/**
* _strlen - Calculate the length of a string
* @string: string to be counted.
* Return: number of bytes in the string.
*/
int _strlen(char *string)
{
int counter = 0;
while (*(string + counter))
counter++;
return (counter);
}
/**
* _atoi - Convert a string to an integer
* @string: string to convert
* Return: the converted value, 0 on failure.
*/
int _atoi(char *string)
{
int a = 0, b = 0, d = 0, c = 0;
unsigned int conversion = 0;
while (string[a] != '\0')
d++, a++;
for (b = 0 + c; b <= d - 1; b++)
{
if (string[b] >= 48 && string[b] <= 57)
{
conversion = ((conversion * 10) + string[b] - '0');
if (string[b + 1] < 48 || string[b + 1] > 57)
break;
}
}
c = 0;
b = 0;
while (c != 1)
{
if (string[b] >= 48 && string[b] <= 57)
c = 1;
if (string[b] == 45)
conversion = (conversion * -1);
b++;
}
return (conversion);
}