-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr.c
113 lines (89 loc) · 1.29 KB
/
str.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "shell.h"
/**
* _strdup - duplicate strings
* @string: string
* Return: string
*/
char *_strdup(const char *string)
{
char *ptr;
int i, l = 0;
if (string == NULL)
return (NULL);
while (*string != '\0')
{
l++;
string++;
}
string = string - l;
ptr = malloc(sizeof(char) * (l + 1));
if (ptr == NULL)
return (NULL);
for (i = 0; i <= l; i++)
{
ptr[i] = string[i];
}
return (ptr);
}
/**
* _strcmp - entry point
* @s1: char
* @s2: char
* Return: (s1 - s2) or (0)
*/
int _strcmp(char *s1, char *s2)
{
while ((*s1 != '\0' && *s2 != '\0') && *s1 == *s2)
{
s1++;
s2++;
}
return (*s1 - *s2);
}
/**
* _strlen - entry point
* @s: character to be counted
* Return: l
*/
int _strlen(char *s)
{
int l;
for (l = 0; *s != '\0'; s++)
{
l++;
}
return (l);
}
/**
* _strcat - entry point
* @dest: char
* @src: source
* Return: dest
*/
char *_strcat(char *dest, char *src)
{
int destlen = _strlen(dest);
int i;
for (i = 0; src[i] != '\0'; i++)
dest[destlen + i] = src[i];
dest[destlen + i] = '\0';
return (dest);
}
/**
* _strcpy - entry point
* @dest: char
* @src: source
* Return: dest if success
*/
char *_strcpy(char *dest, char *src)
{
int i;
i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = src[i];
return (dest);
}