-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmore_strings.c
More file actions
86 lines (82 loc) · 1.49 KB
/
Copy pathmore_strings.c
File metadata and controls
86 lines (82 loc) · 1.49 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
#include "shell.h"
/**
* _strcmp - compares two strings
* @s1: pointer to string s1
* @s2: pointer to string s2
* Return: difference in value of first char that's different
*/
int _strcmp(char *s1, char *s2)
{
int i, dif;
i = 0;
while (s1[i] != '\0' || s2[i] != '\0')
{
if (s1[i] != s2[i])
{
dif = s1[i] - s2[i];
return (dif);
}
i++;
}
if (s1[i] == s2[i] && s1[i] == '\0')
dif = 0;
return (dif);
}
/**
* _strchr - locates character in string
* @str: pointer to a string
* @c: char to find
* Return: pointer to the matched character or NULL
*/
char *_strchr(char *str, char c)
{
int i;
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] == c)
return ((char *)(str + i));
}
return (NULL);
}
/**
* len_to_char - finds string length to passed char
* @str: pointer to a string
* @c: char to find
* Return: length of string before char or 0 if not found
*/
int len_to_char(char *str, char c)
{
int i;
i = 0;
while (str[i] != c && str[i] != '\0')
i++;
if (str[i] == '\0')
return (0);
return (i);
}
/**
* _atoi - converts string to integer
* @s: pointer to string to print
* Return: number or -1 on error
*/
int _atoi(char *s)
{
int size, number, exponent, i;
size = _strlen(s);
exponent = 1;
for (i = 1; i < size; i++)
exponent *= 10;
number = 0;
for (i = 0; s[i] != '\0'; i++)
{
if (s[i] >= '0' && s[i] <= '9')
number += (s[i] - '0') * exponent;
else
{
perror("wrong input for exit status\n");
return (-1);
}
exponent /= 10;
}
return (number);
}