-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings_f1.c
99 lines (86 loc) · 1.65 KB
/
strings_f1.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
#include "shell.h"
/**
* _strlen - Return the length of string
* @s: pointer of type char *
* Return: str_Length
*/
int _strlen(const char *s)
{
int i = 0;
while (*s++)
i++;
return (i);
}
/**
* _strstr - Find the first occurrence of the substring (needle) in (haystack)
* @haystack: pointer type char
* @needle: pointer type char
* Return: Pointer to the beginning of the located substring, or NULL
*/
char *_strstr(const char *haystack, const char *needle)
{
while (*needle)
{
if (*needle != *haystack)
return (NULL);
needle++;
haystack++;
}
return ((char *)haystack);
}
/**
* _strcmp - Compare two strings
* @s1: pointer to the first string
* @s2: pointer to the second string
* Return: (0) if equal, (-1) if s1 < s2, (1) if s2 < s1, or the difference
*/
int _strcmp(char *s1, char *s2)
{
while (*s1 && *s2 && *s1 == *s2)
{
s1++;
s2++;
}
if (!*s1 && !*s2)
return (0);
if (!*s1) /* s1 is shorter than s2 */
return (-1);
if (!*s2) /* s2 is shorter than s1 */
return (1);
return (*s1 - *s2);
}
/**
* _strcat - Append a string to another string
* @dest: pointer of type char *
* @src: pointer of type char *
* Return: the pointer to dest.
*/
char *_strcat(char *dest, char *src)
{
char *new_dest = dest;
while (*dest)
dest++;
while (*src)
*dest++ = *src++;
*dest = *src;
return (new_dest);
}
/**
* _strcpy - Copy string from src to dest
* @dest: data of type char *
* @src: data of type char *
* Return: the pointer to dest.
*/
char *_strcpy(char *dest, char *src)
{
int i = 0;
if (dest == src || *src == '\0')
return (dest);
while (src[i])
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}