-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle_strings.c
More file actions
129 lines (109 loc) · 1.95 KB
/
handle_strings.c
File metadata and controls
129 lines (109 loc) · 1.95 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "main.h"
/**
* _strdup - Duplicate a string.
* @str: The string to be duplicated.
*
* Return:
* - On success, returns a pointer to a newly allocated space in memory
* containing a duplicate of the given string.
* - If memory allocation fails or the input string is NULL, returns NULL.
*/
char *_strdup(const char *str)
{
char *ptr;
int i, len = 0;
if (str == NULL)
return (NULL);
while (*str != '\0')
{
len++;
str++;
}
str = str - len;
ptr = malloc(sizeof(char) * (len + 1));
if (ptr == NULL)
return (NULL);
for (i = 0; i <= len; i++)
ptr[i] = str[i];
return (ptr);
}
/**
* _strcmp - Compare two strings.
* @s1: The first string.
* @s2: The second string.
*
* Return:
* - 0 if the strings are equal.
* - A positive value if the first character that does not match has a greater
* value in s1 than in s2.
* - A negative value if the first character that does not match has a greater
* value in s2 than in s1.
*/
int _strcmp(char *s1, char *s2)
{
while (*s1 != '\0' && *s2 != '\0')
{
if (*s1 != *s2)
{
return (*s1 - *s2);
}
s1++;
s2++;
}
return (0);
}
/**
* _strlen - Calculate the length of a string.
* @s: The string.
*
* Return:
* - The length of the string.
*/
int _strlen(char *s)
{
int vari = 0;
while (s[vari])
vari++;
return (vari);
}
/**
* _strcpy - Copy a string.
* @dest: The destination string.
* @src: The source string.
*
* Return:
* - A pointer to the destination string.
*/
char *_strcpy(char *dest, char *src)
{
int x = 0;
int y;
while (*(src + x) != '\0')
x++;
for (y = 0; y < x; y++)
dest[y] = src[y];
dest[x] = '\0';
return (dest);
}
/**
* _strcat - Concatenate two strings.
* @dest: The destination string.
* @src: The source string.
*
* Return:
* - A pointer to the destination string.
*/
char *_strcat(char *dest, char *src)
{
char *p = dest;
while (*p)
p++;
while (*src)
{
*p = *src;
p++;
src++;
}
*p = '\0';
return (dest);
}