-
Notifications
You must be signed in to change notification settings - Fork 3
/
string1.c
75 lines (70 loc) · 1.34 KB
/
string1.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
#include "shell.h"
/**
* _strlen - function that prints length of a string
* @r: string whose length to be printed
*
* Return: int length of string
*/
int _strlen(char *r)
{
int j = 0;
if (!r)
return (0);
while (*r++)
j++;
return (j);
}
/**
* starts_with - function that checks whether needle begins with haystack
* @haystack: string to be searched for
* @needle: the substring to be sought
*
* Return: address of next character of haystack or NULL if otherwise
*/
char *starts_with(const char *haystack, const char *needle)
{
while (*needle)
if (*needle++ != *haystack++)
return (NULL);
return ((char *)haystack);
}
/**
* _strcat - function that concatenates desti and srce
* @desti: destination buffer
* @srce: source buffer
*
* Return: ptr to desti buffer
*/
char *_strcat(char *desti, char *srce)
{
char *p = desti;
while (*desti)
desti++;
while (*srce)
*desti++ = *srce++;
*desti = *srce;
return (p);
}
/**
* _strcmp - function that compares of two strangs.
* @str1: first string
* @str2: second string
*
* Return: negative if str1 < str2,
* positive if str1 > str2,
* zero if str1 == str2
*/
int _strcmp(char *str1, char *str2)
{
while (*str1 && *str2)
{
if (*str1 != *str2)
return (*str1 - *str2);
str1++;
str2++;
}
if (*str1 == *str2)
return (0);
else
return (*str1 < *str2 ? -1 : 1);
}