-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_functions.c
More file actions
101 lines (86 loc) · 1.54 KB
/
string_functions.c
File metadata and controls
101 lines (86 loc) · 1.54 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
#include "shell.h"
/**
* _strcmp - compares two strings
* @s1: compared to s2;
* @s2: compared to s1;
* Return: returns difference between strings
*/
int _strcmp(char *s1, char *s2)
{
int i = 0, output;
while (*(s1 + i) == *(s2 + i) && *(s1 + i) != '\0')
i++;
output = (*(s1 + i) - *(s2 + i));
return (output);
}
/**
* _strlen - returns the length of a string
* @s: string passed
* Return: returns length of string passed
*/
int _strlen(char *s)
{
int count = 0;
while (*s != '\0')
{
count++;
s++;
}
return (count);
}
/**
* _strncmp - compares two strings up to n bytes
* @s1: compared to s2
* @s2: compared to s1
* @n: number of bytes
* Return: difference between s1 and s2
*/
int _strncmp(char *s1, char *s2, int n)
{
int i;
for (i = 0; s1[i] && s2[i] && i < n; i++)
{
if (s1[i] != s2[i])
return (s1[i] - s2[i]);
}
return (0);
}
/**
* _strdup - dupicates string
* @s: to be duplicated
* Return: pointer to duplicate string
*/
char *_strdup(char *s)
{
char *ptr;
int i, len;
if (s == NULL)
return (NULL);
len = _strlen(s);
ptr = malloc(sizeof(char) * (len + 1));
if (!ptr)
return (NULL);
for (i = 0; *s != '\0'; s++, i++)
ptr[i] = s[0];
ptr[i++] = '\0';
return (ptr);
}
/**
* _strchr - locates a character in a string
* @s: string to be checked
* @c: character to be located
* Return: returns pointer to first occurence of character
* or NULL if character not found
*/
char *_strchr(char *s, char c)
{
while (*s)
{
if (*s == c)
return (s);
s++;
}
if (!c)
return (s);
return (NULL);
}