-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions3.c
106 lines (98 loc) · 1.86 KB
/
functions3.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
#include "shell.h"
/**
* _strcmp - Compares two strings.
* @s1: The first string to compare.
* @s2: The second string to compare.
* Return: An integer indicating the comparison result.
*/
int _strcmp(const char *s1, const char *s2)
{
while (*s1 != '\0' || *s2 != '\0')
{
if (*s1 != *s2)
{
return (*s1 - *s2);
}
s1++;
s2++;
}
return (0);
}
/**
* _strncmp - Compares two strings up to a specified length.
* @s1: The first string to compare.
* @s2: The second string to compare.
* @n: The maximum number of characters to compare.
* Return: An integer indicating the comparison result.
*/
int _strncmp(const char *s1, const char *s2, size_t n)
{
while (n > 0 && (*s1 != '\0' || *s2 != '\0'))
{
if (*s1 != *s2)
{
return (*s1 - *s2);
}
s1++;
s2++;
n--;
}
return (0);
}
/**
* _strncpy - Copies a string up to a specified length.
* @dest: The destination buffer.
* @src: The source string to be copied.
* @n: The maximum number of characters to copy.
* Return: A pointer to the destination buffer.
*/
char *_strncpy(char *dest, const char *src, size_t n)
{
char *original_dest = dest;
while (n > 0 && *src != '\0')
{
*dest = *src;
dest++;
src++;
n--;
}
while (n > 0)
{
*dest = '\0';
dest++;
n--;
}
return (original_dest);
}
/**
* _strcpy - Copies a string from source to destination.
* @dest: The destination buffer.
* @src: The source string to be copied.
* Return: A pointer to the destination buffer.
*/
char *_strcpy(char *dest, const char *src)
{
char *original_dest = dest;
while (*src != '\0')
{
*dest = *src;
dest++;
src++;
}
*dest = '\0';
return (original_dest);
}
/**
* _strlen - Calculates the length of a string.
* @str: The string to be measured.
* Return: The length of the string.
*/
size_t _strlen(const char *str)
{
size_t len = 0;
while (str[len] != '\0')
{
len++;
}
return (len);
}