-
Notifications
You must be signed in to change notification settings - Fork 0
/
_string_functions.c
121 lines (110 loc) · 2.69 KB
/
_string_functions.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
114
115
116
117
118
119
120
121
#include "main.h"
/**
* _strlen - returns the length of a string.
* @s: the string to check.
*
* Return: length of a string.
*/
int _strlen(char *s)
{
int length = 0;
/* Count the length until null terminator is reached */
while (*s)
{
length++;
s++;
}
return (length);
}
/**
* _strcpy - copies the string pointed to by src, including the terminating
* null byte (\0), to the buffer pointed to by dest.
* @src: the string to copy.
* @dest: the string to paste.
*
* Return: a pointer to dest.
*/
char *_strcpy(char *dest, char *src)
{
int i = 0;
/* If src is invalid, return dest */
if (src == NULL)
return (dest);
/* Copy characters from src to dest until null terminator is reached */
while (*(src + i) != '\0')
{
*(dest + i) = *(src + i);
i++;
}
/* Add null terminator to dest */
*(dest + i) = '\0';
return (dest);
}
/**
* _strdup - duplicates a string.
* @line: the string to be duplicated.
*
* Return: a pointer to the new duplicated string,
* otherwise NULL if it fails.
*/
char *_strdup(char *line)
{
size_t length;
char *new_line;
/* Check for invalid argument */
if (line == NULL)
return (NULL);
/*Calculate the length of input string, including the null terminator*/
length = _strlen(line) + 1;
/* Allocate memory for the new string */
new_line = malloc(length);
if (new_line == NULL)
return (NULL);
/* Copy the input string to the new string using @_strcpy function */
_strcpy(new_line, line);
return (new_line);
}
/**
* _strcmp - compares two strings.
* @s1: the first string.
* @s2: the second string.
*
* Return: an integer <0 if s1 less than s2,
* an integer =0 if s1 match s2,
* an ineteger >0 if s1 greater than s2.
*/
int _strcmp(char *s1, char *s2)
{
/* Loop through the two strings character by character if are equal */
while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)
{
s1++;
s2++;
}
/*
* If the strings match up to this point, return 0,
* otherwise if the characters at the current position of s1 and s2 are
* different, return the difference between them.
*/
return (*s1 - *s2);
}
/**
* _strncmp - compares the first n characters of two strings s1 and s2.
* @s1: the first string.
* @s2: the second string.
* @n: the maximum number of characters to compare between the two strings.
*
* Return: an integer less than, equal to, or greater than zero if s1 is
* found, respectively, to be less than, to match, or be greater than s2.
*/
int _strncmp(const char *s1, const char *s2, int n)
{
/* Compare characters until n is reached or mismatch is found */
while (n-- && *s1 != '\0' && (*s1 == *s2))
{
s1++;
s2++;
}
/* return the difference between the first non-matching characters */
return ((n < 0) ? 0 : (*s1 - *s2));
}