-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdfunc2.c
More file actions
103 lines (82 loc) · 1.67 KB
/
stdfunc2.c
File metadata and controls
103 lines (82 loc) · 1.67 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
#include "shell.h"
/**
* *_strcpy - copies the string pointed to by src, including \0.
*@src: pointer.
*@dest: pointer.
*Return: the pointer to dest.
*/
char *_strcpy(char *dest, char *src)
{
int i, length;
for (length = 0; src[length] != '\0'; length++)
{
}
for (i = 0; i <= length ; i++)
{
dest[i] = src[i];
}
return (dest);
}
/**
* _strdup - This function duplicate a string in new memory.
* @str: This is a string pointer
* Return: returns a pointer to malloc
*/
char *_strdup(char *str)
{
int i, len = 0;
char *sptr;
if (str == NULL)
return (NULL);
while (str[len] != '\0')
len++;
sptr = (char *) malloc(len * sizeof(char) + 1);
if (sptr == NULL)
return (NULL);
for (i = 0; str[i] != '\0'; i++)
{
sptr[i] = str[i];
}
sptr[i++] = '\0';
return (sptr);
}
/**
* _strlen - calculates for the lenght of a string
*
* @s: The function parameter
*
* Return: Always 0 (Success)
*
*/
int _strlen(const char *s)
{
int len = 0;
while (s[len] != '\0')
len++;
return (len);
}
/**
* _strncmp - Compares first n bytes of a string to another
* @s1: First string
* @s2: Second string
* @n: Number of bytes/characters to compare
* Return: 0 if strings are same, non-zero if not
*/
int _strncmp(const char *s1, const char *s2, int n)
{
int i = 0;
/* Compare each element of s1 with s2 */
for (i = 0; (s1[i] != '\0' || s2[i] != '\0') && i < n; i++)
{
/* Return 0 if s1 and s2 are the same */
if (s1[i] == s2[i])
{
if (s1[i + 1] == '\0' || s2[i + 1] == '\0' || i + 1 == n)
return (0);
}
else if (s1[i] < s2[i] || s2[i] < s1[i])
break;
}
/* Return differene bwtween strings if they aren't same */
return (s1[i] - s2[i]);
}